TemplateParser: Pass FLAG_MUSTACHELOOKUP to enable parent context access
authorRoan Kattouw <roan.kattouw@gmail.com>
Fri, 31 Aug 2018 03:18:44 +0000 (20:18 -0700)
committerRoan Kattouw <roan.kattouw@gmail.com>
Tue, 4 Sep 2018 19:39:34 +0000 (12:39 -0700)
This allows variables defined in an outer context to be used in inner
contexts. For example:

<h2>{{foo}}</h2>
<ul>
{{#things}}
    <!-- bar is a property of each thing, foo is an outer variable -->
    <li>{{foo}} is a {{bar}}</li>
{{/things}}
</ul>

Bug: T203209
Change-Id: Ib0ae0fb0b4be6b161f548c79db6fb6f4b831f7c1

includes/TemplateParser.php
tests/phpunit/data/templates/parentvars.mustache [new file with mode: 0644]
tests/phpunit/includes/TemplateParserTest.php

index 4210a96..75494b1 100644 (file)
@@ -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 (file)
index 0000000..aa732c0
--- /dev/null
@@ -0,0 +1,4 @@
+{{foo}}
+{{#bar}}
+       {{foo}} {{baz}}
+{{/bar}}
index ccccf0f..cb321bb 100644 (file)
@@ -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"
+                       ]
                ];
        }