From 3c06d76521a85c0da35b836e3510d316414b7028 Mon Sep 17 00:00:00 2001 From: Roan Kattouw Date: Thu, 30 Aug 2018 20:18:44 -0700 Subject: [PATCH] TemplateParser: Pass FLAG_MUSTACHELOOKUP to enable parent context access This allows variables defined in an outer context to be used in inner contexts. For example:

{{foo}}

Bug: T203209 Change-Id: Ib0ae0fb0b4be6b161f548c79db6fb6f4b831f7c1 --- includes/TemplateParser.php | 8 +++++--- tests/phpunit/data/templates/parentvars.mustache | 4 ++++ tests/phpunit/includes/TemplateParserTest.php | 11 +++++++++++ 3 files changed, 20 insertions(+), 3 deletions(-) create mode 100644 tests/phpunit/data/templates/parentvars.mustache diff --git a/includes/TemplateParser.php b/includes/TemplateParser.php index 4210a965ba..75494b1ed8 100644 --- a/includes/TemplateParser.php +++ b/includes/TemplateParser.php @@ -41,9 +41,7 @@ class TemplateParser { /** * @var int Compilation flags passed to LightnCandy */ - // Do not add more flags here without discussion. - // If you do add more flags, be sure to update unit tests as well. - protected $compileFlags = LightnCandy::FLAG_ERROR_EXCEPTION; + protected $compileFlags; /** * @param string|null $templateDir @@ -52,6 +50,10 @@ class TemplateParser { public function __construct( $templateDir = null, $forceRecompile = false ) { $this->templateDir = $templateDir ?: __DIR__ . '/templates'; $this->forceRecompile = $forceRecompile; + + // Do not add more flags here without discussion. + // If you do add more flags, be sure to update unit tests as well. + $this->compileFlags = LightnCandy::FLAG_ERROR_EXCEPTION | LightnCandy::FLAG_MUSTACHELOOKUP; } /** diff --git a/tests/phpunit/data/templates/parentvars.mustache b/tests/phpunit/data/templates/parentvars.mustache new file mode 100644 index 0000000000..aa732c0753 --- /dev/null +++ b/tests/phpunit/data/templates/parentvars.mustache @@ -0,0 +1,4 @@ +{{foo}} +{{#bar}} + {{foo}} {{baz}} +{{/bar}} diff --git a/tests/phpunit/includes/TemplateParserTest.php b/tests/phpunit/includes/TemplateParserTest.php index ccccf0f949..cb321bbe36 100644 --- a/tests/phpunit/includes/TemplateParserTest.php +++ b/tests/phpunit/includes/TemplateParserTest.php @@ -105,6 +105,17 @@ class TemplateParserTest extends MediaWikiTestCase { false, 'Exception', ], + [ + 'parentvars', + [ + 'foo' => 'f', + 'bar' => [ + [ 'baz' => 'x' ], + [ 'baz' => 'y' ] + ] + ], + "f\n\n\tf x\n\n\tf y\n\n" + ] ]; } -- 2.20.1