Przeglądaj źródła

Respect delimiter changes in lambda helper.

See janl/mustache.js#489
Justin Hileman 10 lat temu
rodzic
commit
9cc21d63f7

+ 6 - 3
src/Mustache/Compiler.php

@@ -328,7 +328,7 @@ class Mustache_Compiler
             $buffer = \'\';
             if (%s) {
                 $source = %s;
-                $result = call_user_func($value, $source, $this->lambdaHelper);
+                $result = call_user_func($value, $source, %s);
                 if (strpos($result, \'{{\') === false) {
                     $buffer .= $result;
                 } else {
@@ -370,15 +370,18 @@ class Mustache_Compiler
         $callable = $this->getCallable();
 
         if ($otag !== '{{' || $ctag !== '}}') {
-            $delims = ', ' . var_export(sprintf('{{= %s %s =}}', $otag, $ctag), true);
+            $delimTag = var_export(sprintf('{{= %s %s =}}', $otag, $ctag), true);
+            $helper = sprintf('$this->lambdaHelper->withDelimiters(%s)', $delimTag);
+            $delims = ', ' . $delimTag;
         } else {
+            $helper = '$this->lambdaHelper';
             $delims = '';
         }
 
         $key = ucfirst(md5($delims . "\n" . $source));
 
         if (!isset($this->sections[$key])) {
-            $this->sections[$key] = sprintf($this->prepare(self::SECTION), $key, $callable, $source, $delims, $this->walk($nodes, 2));
+            $this->sections[$key] = sprintf($this->prepare(self::SECTION), $key, $callable, $source, $helper, $delims, $this->walk($nodes, 2));
         }
 
         if ($arg === true) {

+ 17 - 2
src/Mustache/LambdaHelper.php

@@ -20,17 +20,20 @@ class Mustache_LambdaHelper
 {
     private $mustache;
     private $context;
+    private $delims;
 
     /**
      * Mustache Lambda Helper constructor.
      *
      * @param Mustache_Engine  $mustache Mustache engine instance.
      * @param Mustache_Context $context  Rendering context.
+     * @param string           $delims   Optional custom delimiters, in the format `{{= <% %> =}}`. (default: null)
      */
-    public function __construct(Mustache_Engine $mustache, Mustache_Context $context)
+    public function __construct(Mustache_Engine $mustache, Mustache_Context $context, $delims = null)
     {
         $this->mustache = $mustache;
         $this->context  = $context;
+        $this->delims   = $delims;
     }
 
     /**
@@ -43,7 +46,19 @@ class Mustache_LambdaHelper
     public function render($string)
     {
         return $this->mustache
-            ->loadLambda((string) $string)
+            ->loadLambda((string) $string, $this->delims)
             ->renderInternal($this->context);
     }
+
+    /**
+     * Get a Lambda Helper with custom delimiters.
+     *
+     * @param string $delims Custom delimiters, in the format `{{= <% %> =}}`.
+     *
+     * @return Mustache_LambdaHelper
+     */
+    public function withDelimiters($delims)
+    {
+        return new self($this->mustache, $this->context, $delims);
+    }
 }

+ 13 - 0
test/Mustache/Test/FiveThree/Functional/LambdaHelperTest.php

@@ -36,4 +36,17 @@ class Mustache_Test_FiveThree_Functional_LambdaHelperTest extends PHPUnit_Framew
         $this->assertEquals('Mario', $one->render($foo));
         $this->assertEquals('MARIO', $two->render($foo));
     }
+
+    public function testSectionLambdaHelperRespectsDelimiterChanges()
+    {
+        $tpl = $this->mustache->loadTemplate("{{=<% %>=}}\n<%# bang %><% value %><%/ bang %>");
+
+        $data = new StdClass();
+        $data->value = 'hello world';
+        $data->bang = function ($text, $mustache) {
+            return $mustache->render($text) . '!';
+        };
+
+        $this->assertEquals('hello world!', $tpl->render($data));
+    }
 }