mirror of
https://github.com/phpbb/phpbb.git
synced 2025-06-28 14:18:52 +00:00
[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
This commit is contained in:
parent
35b628f737
commit
5f03321fac
2 changed files with 71 additions and 7 deletions
|
@ -19,8 +19,9 @@ class phpbb_template_twig_lexer extends Twig_Lexer
|
||||||
{
|
{
|
||||||
public function tokenize($code, $filename = null)
|
public function tokenize($code, $filename = null)
|
||||||
{
|
{
|
||||||
$valid_starting_tokens = array(
|
// Our phpBB tags
|
||||||
// Commented out tokens are handled separately from the main replace
|
// Commented out tokens are handled separately from the main replace
|
||||||
|
$phpbb_tags = array(
|
||||||
/*'BEGIN',
|
/*'BEGIN',
|
||||||
'BEGINELSE',
|
'BEGINELSE',
|
||||||
'END',*/
|
'END',*/
|
||||||
|
@ -39,6 +40,34 @@ class phpbb_template_twig_lexer extends Twig_Lexer
|
||||||
'EVENT',
|
'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}')
|
// Fix tokens that may have inline variables (e.g. <!-- DEFINE $TEST = '{FOO}')
|
||||||
$code = $this->fix_inline_variable_tokens(array(
|
$code = $this->fix_inline_variable_tokens(array(
|
||||||
'DEFINE.+=',
|
'DEFINE.+=',
|
||||||
|
@ -58,16 +87,22 @@ class phpbb_template_twig_lexer extends Twig_Lexer
|
||||||
|
|
||||||
// Replace all of our starting tokens, <!-- TOKEN --> with Twig style, {% TOKEN %}
|
// Replace all of our starting tokens, <!-- TOKEN --> with Twig style, {% TOKEN %}
|
||||||
// This also strips outer parenthesis, <!-- IF (blah) --> becomes <!-- IF blah -->
|
// 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') }}
|
// 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 }}
|
// 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 }}
|
// 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);
|
return parent::tokenize($code, $filename);
|
||||||
}
|
}
|
||||||
|
@ -227,4 +262,33 @@ class phpbb_template_twig_lexer extends Twig_Lexer
|
||||||
|
|
||||||
return $code;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -132,7 +132,7 @@ class phpbb_template_twig implements phpbb_template
|
||||||
array(
|
array(
|
||||||
'cache' => $this->cachepath,
|
'cache' => $this->cachepath,
|
||||||
'debug' => defined('DEBUG'),
|
'debug' => defined('DEBUG'),
|
||||||
'auto_reload' => (bool) $this->config['load_tplcompile'],
|
'auto_reload' => true,//(bool) $this->config['load_tplcompile'],
|
||||||
'autoescape' => false,
|
'autoescape' => false,
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
Loading…
Add table
Reference in a new issue