diff --git a/phpBB/includes/template/twig/extension.php b/phpBB/includes/template/twig/extension.php index d7400fde4b..a3da61fcdb 100644 --- a/phpBB/includes/template/twig/extension.php +++ b/phpBB/includes/template/twig/extension.php @@ -17,11 +17,24 @@ if (!defined('IN_PHPBB')) class phpbb_template_twig_extension extends Twig_Extension { + /** @var phpbb_user */ + protected $user; + + public function __construct($user) + { + $this->user = $user; + } + public function getName() { return 'phpbb'; } + /** + * Returns the token parser instance to add to the existing list. + * + * @return array An array of Twig_TokenParser instances + */ public function getTokenParsers() { return array( @@ -35,6 +48,11 @@ class phpbb_template_twig_extension extends Twig_Extension ); } + /** + * Returns a list of filters to add to the existing list. + * + * @return array An array of filters + */ public function getFilters() { return array( @@ -42,6 +60,23 @@ class phpbb_template_twig_extension extends Twig_Extension ); } + /** + * Returns a list of global functions to add to the existing list. + * + * @return array An array of global functions + */ + public function getFunctions() + { + return array( + new Twig_SimpleFunction('lang', array($this->user, 'lang')), + ); + } + + /** + * Returns a list of operators to add to the existing list. + * + * @return array An array of operators + */ public function getOperators() { return array( diff --git a/phpBB/includes/template/twig/lexer.php b/phpBB/includes/template/twig/lexer.php index a2afcf18be..1daa6c30c9 100644 --- a/phpBB/includes/template/twig/lexer.php +++ b/phpBB/includes/template/twig/lexer.php @@ -59,6 +59,12 @@ class phpbb_template_twig_lexer extends Twig_Lexer // This also strips outer parenthesis, becomes $code = preg_replace('##', '{% $1 $2 %}', $code); + // Replace all of our language variables, {L_VARNAME}, with Twig style, {{ lang('NAME') }} + $code = preg_replace('#{L_([a-zA-Z0-9_\.]+)}#', '{{ lang(\'$1\') }}', $code); + + // Replace all of our JS escaped language variables, {LA_VARNAME}, with Twig style, {{ lang('NAME')|escape('js') }} + $code = preg_replace('#{LA_([a-zA-Z0-9_\.]+)}#', '{{ lang(\'$1\')|escape(\'js\') }}', $code); + // Replace all of our variables, {VARNAME}, with Twig style, {{ VARNAME }} $code = preg_replace('#{([a-zA-Z0-9_\.]+)}#', '{{ $1 }}', $code); diff --git a/phpBB/includes/template/twig/twig.php b/phpBB/includes/template/twig/twig.php index 65776b7b6e..f5879c6027 100644 --- a/phpBB/includes/template/twig/twig.php +++ b/phpBB/includes/template/twig/twig.php @@ -131,7 +131,11 @@ class phpbb_template_twig implements phpbb_template // @todo remove $this->clear_cache(); - $this->twig->addExtension(new phpbb_template_twig_extension); + $this->twig->addExtension( + new phpbb_template_twig_extension( + $this->user + ) + ); $lexer = new phpbb_template_twig_lexer($this->twig); @@ -430,27 +434,13 @@ class phpbb_template_twig implements phpbb_template { $vars = array(); - // Work-around for now - if (!empty($this->user->lang)) - { - foreach ($this->user->lang as $key => $value) - { - if (!is_string($value)) - { - continue; - } - - $vars['L_' . strtoupper($key)] = $value; - $vars['LA_' . strtoupper($key)] = addslashes($value); - } - } - $vars = array_merge( $vars, $this->context->get_rootref(), $this->context->get_tpldata(), array( 'definition' => new phpbb_template_twig_definition(), + 'user' => $this->user, ) );