From 48cc96b489366641a818888ad45578c2a2948398 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Fri, 14 Jun 2024 23:10:09 +0200 Subject: [PATCH] [ticket/17335] Add lang_raw twig function for templates PHPBB-17335 --- phpBB/phpbb/template/twig/extension.php | 13 +++++++++++++ tests/template/template_test.php | 6 +++--- tests/template/templates/lang_twig.html | 4 ++++ 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/phpBB/phpbb/template/twig/extension.php b/phpBB/phpbb/template/twig/extension.php index 2e920dbe4b..7fde9e2cac 100644 --- a/phpBB/phpbb/template/twig/extension.php +++ b/phpBB/phpbb/template/twig/extension.php @@ -93,6 +93,7 @@ class extension extends \Twig\Extension\AbstractExtension new \Twig\TwigFunction('lang', array($this, 'lang')), new \Twig\TwigFunction('lang_defined', array($this, 'lang_defined')), new \Twig\TwigFunction('lang_js', [$this, 'lang_js']), + new \Twig\TwigFunction('lang_raw', [$this, 'lang_raw']), new \Twig\TwigFunction('get_class', 'get_class'), ); } @@ -214,4 +215,16 @@ class extension extends \Twig\Extension\AbstractExtension return twig_escape_filter($this->environment, call_user_func_array([$this, 'lang'], $args), 'js'); } + + /** + * Get raw value associated with lang key + * + * @param string $key + * + * @return array|string Raw value associated with lang key + */ + public function lang_raw(string $key): array|string + { + return call_user_func_array(array($this->language, 'lang_raw'), [$key]); + } } diff --git a/tests/template/template_test.php b/tests/template/template_test.php index 8faa52d8ac..4fc9858366 100644 --- a/tests/template/template_test.php +++ b/tests/template/template_test.php @@ -365,15 +365,15 @@ class phpbb_template_template_test extends phpbb_template_template_test_case array(), array(), array(), - "VARIABLE\n1_VARIABLE\nVARIABLE\n1_VARIABLE", + "VARIABLE\n1_VARIABLE\nVARIABLE\n1_VARIABLE\nVARIABLE\n1_VARIABLE\nARY_VARIABLE", ), array( 'lang_twig.html', array(), array(), array(), - "Value'\n1 O'Clock\nValue\\u0027\n1\\u0020O\\u0027Clock", - array('VARIABLE' => "Value'", '1_VARIABLE' => "1 O'Clock"), + "Value'\n1 O'Clock\nValue\\u0027\n1\\u0020O\\u0027Clock\nValue'\n1 O'Clock\nfoo|bar", + array('VARIABLE' => "Value'", '1_VARIABLE' => "1 O'Clock", 'ARY_VARIABLE' => ['foo', 'bar']), ), array( 'loop_nested_multilevel_ref.html', diff --git a/tests/template/templates/lang_twig.html b/tests/template/templates/lang_twig.html index bf31012819..a331c7f506 100644 --- a/tests/template/templates/lang_twig.html +++ b/tests/template/templates/lang_twig.html @@ -3,3 +3,7 @@ {{ lang_js('VARIABLE') }} {{ lang_js('1_VARIABLE') }} + +{{ lang_raw('VARIABLE') }} +{{ lang_raw('1_VARIABLE') }} +{{ lang_raw('ARY_VARIABLE')|join('|') }}