phpwcms icon indicating copy to clipboard operation
phpwcms copied to clipboard

Provide Twig Template Engine in Addition to Own Replacement Tags

Open marcus-at-localhost opened this issue 8 years ago • 1 comments

I created a module for myself and got frustrated how inflexible phpwcms own template language is, especially the conditional statements with [REPLACEMENT_TAG_ELSE]. I think it's time to slowly move or offer, something like Twig while keeping the "old" way intact.

This is a quick example with loading template fragments:

<!-- /tmpl.twig -->
{% if AUTHOR %}
<h1>{{ AUTHOR }}</h1>
{% endif %}
// twig is included via composer/autoload.php
$twig = new \Twig_Environment(new \Twig_Loader_String());

$data = [
  'AUTHOR' => 'Mark Twain',
  'TITLE' => 'The Awful German Language'
];

$template = get_file_contents('tmpl.twig');

// render twig
$TMP = $twig->render($template, $data);

// old way to keep old code intact
$TMP = render_cnt_template($TMP, 'AUTHOR', $rss['item_author'] ? $rss['item_author'] : '' );

I think it would help cleaning up core code too.

The only concerns I would have is performance when this is used to render every single content part and as I found out now, Twig_Loader_String is not the recommended way to load template strings: https://stackoverflow.com/questions/31081910/what-to-use-instead-of-twig-loader-string.

Thoughts?

marcus-at-localhost avatar Nov 08 '17 17:11 marcus-at-localhost

This is for something BIGGER and needs refactoring code and logic. You can really see how old rendering inside of phpwcms is. First content parts needs to be split into something generic, kind a bit like I have done with custom fields. I cannot say if I like Twig or not. I don't like the {% %} inside of templates. My favourite would be something <phpwcms:if ></phpwcms:if>. But I know this is not the point ;-)

I agree – templating needs to be more common so you would be able to easily adopt existing templates.

But I would guess overhead of render_cnt_template() is much smaller than Twig on top. But I know it's not perfect and lacks a lot of flexibility in case of looping and so on.

slackero avatar Nov 26 '17 09:11 slackero