Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Let Grav scan/load template files with different extension than *.html.twig #1818

Open
numito opened this issue Jan 4, 2018 · 5 comments
Open

Comments

@numito
Copy link

numito commented Jan 4, 2018

Hello,

Grav is awesome, I have created a plugin to write template files in HAML with Twig integration. It allows to have concise, clear and short templates. It integrates greatly with Grav and Twig. You can get the plugin here: https://github.com/numito/grav-haml
But I figured out an issue, Grav scans only for .html.twig files in theme folders, also the type 'HTML' and '.html.twig' is not a constant and is redefined in many places in the core files. Grav should have a configurable list of template extensions to scan for and load those templates. By default all template should go through the Twig parser, but a plugin could hook for specific template extension allowing additional template languages.
I made a few simple modifications to core files to allow that, but I am not sure they are exhaustive. I would like some recommendations.
These are the modifications I did to make my plugin work:

Line 60 of Grav\Common\Twig\Twig
Add

    public $templateExt = TEMPLATE_EXT;

Ligne 240 modify

 if ($item->modularTwig()) {
                $twig_vars['content'] = $content;
                $template = $item->template() . $this->templateExt;
                $output = $content = $local_twig->render($template, $twig_vars);
 }

Line 352 modify

 if ($ext != $this->templateExt) {
                try {
                    $page->templateFormat('html');
                    $output = $this->twig->render($page->template() . $this->templateExt, $vars + $twig_vars);

                } catch (\Twig_Error_Loader $e) {
                    throw new \RuntimeException($error_msg, 400, $e);
                }
            } else {
                throw new \RuntimeException($error_msg, 400, $e);
 }

Line 72 et 74 de Grav\Common\Page\Types

public function scanTemplates($uri) {
       $options = [
            'compare' => 'Filename',
            'pattern' => '/\.(haml|html)\.twig$/',
            'filters' => [
                'value' => '/\.(haml|html)\.twig$/'
            ],
            'value' => 'Filename',
            'recursive' => false
        ];
} 
@rhukster
Copy link
Member

rhukster commented Jan 4, 2018

i think its not this simple. The admin needs to be looked at also. I would prefer to improve this with something more flexible, not just haml|html but something that lets you set the markup output if its not inline with the request. By default grav will look for .json.twig if you request a URL with .json, and .html.twig if you request .html (or leave it blank as it's default).

This needs to be tackled at a higher level i think. I will think on this a bit more.

@rhukster
Copy link
Member

rhukster commented Jan 4, 2018

Does your HAML plugin require these code changes to work???

@numito
Copy link
Author

numito commented Jan 5, 2018

Hello,

No it works without modifying Grav core files if you add this first line to all files you want to have haml parsed.

{%haml%}

Grav core files modification is only needed if you want the plugin to automatically kick-in when you have *.haml.twig files without adding this first line.
I agree with you a more modular approach would be match better, what I suggested is a quick and dirty hack. Plugins or core should be able to register file types + extension + rendering/templating engine. This would allow to have alternative templating engines. This would allow to also change the default template extension which is now .html.twig. I could also modify the plugin to use the extension haml.html.twig.

@jbanety
Copy link
Contributor

jbanety commented May 30, 2018

Hi,
I have the same issue implementing Grav AMP templating for a theme (with files like ".amp.twig".)
My main page is default.amp.twig and Grav will load my modulars as .html.twig
The TEMPLATE_EXT const should be at least a variable editable by a plugin.

@numito
Copy link
Author

numito commented Apr 1, 2019

Hi Everyone,

Just wanted to know what is the status of this request ? Do you think we can manage this in 1.6 ?

Best
Numa

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants