aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNathaniel Guse <nathaniel.guse@gmail.com>2013-07-04 12:44:12 -0500
committerNathaniel Guse <nathaniel.guse@gmail.com>2013-07-04 12:44:12 -0500
commit5f03321fac6ce888cb870e158c685bf839c25f07 (patch)
tree537c70d03baebc5faa35765029cc1df1f45c8c62
parent35b628f737014eec0872ed7d0d140f11621af261 (diff)
downloadforums-5f03321fac6ce888cb870e158c685bf839c25f07.tar
forums-5f03321fac6ce888cb870e158c685bf839c25f07.tar.gz
forums-5f03321fac6ce888cb870e158c685bf839c25f07.tar.bz2
forums-5f03321fac6ce888cb870e158c685bf839c25f07.tar.xz
forums-5f03321fac6ce888cb870e158c685bf839c25f07.zip
[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 --> = {% block foo %]. All tags are the same as the Twig tag names, but are in uppercase. PHPBB3-11598
-rw-r--r--phpBB/includes/template/twig/lexer.php76
-rw-r--r--phpBB/includes/template/twig/twig.php2
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. <!-- DEFINE $TEST = '{FOO}')
$code = $this->fix_inline_variable_tokens(array(
'DEFINE.+=',
@@ -58,16 +87,22 @@ class phpbb_template_twig_lexer extends Twig_Lexer
// Replace all of our starting tokens, <!-- TOKEN --> with Twig style, {% TOKEN %}
// This also strips outer parenthesis, <!-- IF (blah) --> becomes <!-- IF blah -->
- $code = preg_replace('#<!-- (' . implode('|', $valid_starting_tokens) . ')(?: (.*?) ?)?-->#', '{% $1 $2 %}', $code);
+ $code = preg_replace('#<!-- (' . implode('|', $phpbb_tags) . ')(?: (.*?) ?)?-->#', '{% $1 $2 %}', $code);
+
+ // Replace all of our twig masks with Twig code (e.g. <!-- BLOCK .+ --> 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. <!-- BLOCK foo --> 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('#<!-- (' . implode('|', $twig_tags) . ')(.*?)-->#',$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,
)
);