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

Provide Twig Template Engine in Addition to Own Replacement Tags #227

Open
marcus-at-localhost opened this issue Nov 8, 2017 · 1 comment

Comments

@marcus-at-localhost
Copy link
Contributor

marcus-at-localhost commented Nov 8, 2017

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 marcus-at-localhost changed the title Provide Twig Template Engine in Addition to Homebrew Replacement Tags Provide Twig Template Engine in Addition to Own Replacement Tags Nov 8, 2017
@slackero
Copy link
Owner

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.

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

2 participants