From 5f03321fac6ce888cb870e158c685bf839c25f07 Mon Sep 17 00:00:00 2001 From: Nathaniel Guse Date: Thu, 4 Jul 2013 12:44:12 -0500 Subject: [PATCH] [feature/twig] Support using Twig filters on {VAR}, add masks for Twig tags Now we can do {L_TITLE|upper}, {SITENAME|lower}, etc We can also use all the Twig tags in our own syntax. E.g. = {% block foo %]. All tags are the same as the Twig tag names, but are in uppercase. PHPBB3-11598 --- phpBB/includes/template/twig/lexer.php | 76 ++++++++++++++++++++++++-- phpBB/includes/template/twig/twig.php | 2 +- 2 files changed, 71 insertions(+), 7 deletions(-) diff --git a/phpBB/includes/template/twig/lexer.php b/phpBB/includes/template/twig/lexer.php index c7ab1590f5..50ef403231 100644 --- a/phpBB/includes/template/twig/lexer.php +++ b/phpBB/includes/template/twig/lexer.php @@ -19,8 +19,9 @@ class phpbb_template_twig_lexer extends Twig_Lexer { public function tokenize($code, $filename = null) { - $valid_starting_tokens = array( - // Commented out tokens are handled separately from the main replace + // Our phpBB tags + // Commented out tokens are handled separately from the main replace + $phpbb_tags = array( /*'BEGIN', 'BEGINELSE', 'END',*/ @@ -39,6 +40,34 @@ class phpbb_template_twig_lexer extends Twig_Lexer 'EVENT', ); + // Twig tag masks + $twig_tags = array( + 'autoescape', + 'endautoescape', + 'block', + 'endblock', + 'use', + 'extends', + 'embed', + 'filter', + 'endfilter', + 'flush', + 'for', + 'endfor', + 'macro', + 'endmacro', + 'import', + 'from', + 'sandbox', + 'endsandbox', + 'set', + 'endset', + 'spaceless', + 'endspaceless', + 'verbatim', + 'endverbatim', + ); + // Fix tokens that may have inline variables (e.g. with Twig style, {% TOKEN %} // This also strips outer parenthesis, becomes - $code = preg_replace('##', '{% $1 $2 %}', $code); + $code = preg_replace('##', '{% $1 $2 %}', $code); + + // Replace all of our twig masks with Twig code (e.g. with {% block $1 %}) + $code = $this->replace_twig_tag_masks($code, $twig_tags); // Replace all of our language variables, {L_VARNAME}, with Twig style, {{ lang('NAME') }} - $code = preg_replace('#{L_([a-zA-Z0-9_\.]+)}#', '{{ lang(\'$1\') }}', $code); + // Appends any filters after lang() + $code = preg_replace('#{L_([a-zA-Z0-9_\.]+)(\|[^}]+)?}#', '{{ lang(\'$1\')$2 }}', $code); // Replace all of our escaped language variables, {LA_VARNAME}, with Twig style, {{ lang('NAME')|addslashes }} - $code = preg_replace('#{LA_([a-zA-Z0-9_\.]+)}#', '{{ lang(\'$1\')|addslashes }}', $code); + // Appends any filters after lang(), but before addslashes + $code = preg_replace('#{LA_([a-zA-Z0-9_\.]+)(\|[^}]+)?}}#', '{{ lang(\'$1\')$2|addslashes }}', $code); // Replace all of our variables, {VARNAME}, with Twig style, {{ VARNAME }} - $code = preg_replace('#{([a-zA-Z0-9_\.]+)}#', '{{ $1 }}', $code); + // Appends any filters + $code = preg_replace('#{([a-zA-Z0-9_\.]+)(\|[^}]+)?}#', '{{ $1$2 }}', $code); return parent::tokenize($code, $filename); } @@ -227,4 +262,33 @@ class phpbb_template_twig_lexer extends Twig_Lexer return $code; } + + /** + * Replace Twig tag masks with Twig tag calls + * + * E.g. with {% block foo %} + * + * @param string $code + * @param array $twig_tags All tags we want to create a mask for + * @return string + */ + protected function replace_twig_tag_masks($code, $twig_tags) + { + $callback = function ($matches) + { + $matches[1] = strtolower($matches[1]); + + return "{% {$matches[1]}{$matches[2]}%}"; + }; + + foreach ($twig_tags as &$tag) + { + $tag = strtoupper($tag); + } + + // twig_tags is an array of the twig tags, which are all lowercase, but we use all uppercase tags + $code = preg_replace_callback('##',$callback, $code); + + return $code; + } } diff --git a/phpBB/includes/template/twig/twig.php b/phpBB/includes/template/twig/twig.php index 621bfe0f4f..47e346ad1e 100644 --- a/phpBB/includes/template/twig/twig.php +++ b/phpBB/includes/template/twig/twig.php @@ -132,7 +132,7 @@ class phpbb_template_twig implements phpbb_template array( 'cache' => $this->cachepath, 'debug' => defined('DEBUG'), - 'auto_reload' => (bool) $this->config['load_tplcompile'], + 'auto_reload' => true,//(bool) $this->config['load_tplcompile'], 'autoescape' => false, ) );