From b9f04e5d26934cf07fa819378d468b456614419a Mon Sep 17 00:00:00 2001 From: Justin Hileman Date: Sun, 2 Jul 2017 22:20:51 -0700 Subject: [PATCH] Custom delimiters tweaks. * Add basic test for custom delimiters * Keep default delims backwards compatible --- src/Mustache/Engine.php | 4 ++-- test/Mustache/Test/EngineTest.php | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/Mustache/Engine.php b/src/Mustache/Engine.php index 41cb920..f998eba 100644 --- a/src/Mustache/Engine.php +++ b/src/Mustache/Engine.php @@ -54,7 +54,7 @@ class Mustache_Engine private $logger; private $strictCallables = false; private $pragmas = array(); - private $delimiters = '{{ }}'; + private $delimiters; // Services private $tokenizer; @@ -621,7 +621,7 @@ class Mustache_Engine // Keep this list in alphabetical order :) $chunks = array( 'charset' => $this->charset, - 'delimiters' => $this->delimiters, + 'delimiters' => $this->delimiters ? $this->delimiters : '{{ }}', 'entityFlags' => $this->entityFlags, 'escape' => isset($this->escape) ? 'custom' : 'default', 'key' => ($source instanceof Mustache_Source) ? $source->getKey() : 'source', diff --git a/test/Mustache/Test/EngineTest.php b/test/Mustache/Test/EngineTest.php index eb5a27a..87a01a7 100644 --- a/test/Mustache/Test/EngineTest.php +++ b/test/Mustache/Test/EngineTest.php @@ -349,6 +349,23 @@ class Mustache_Test_EngineTest extends Mustache_Test_FunctionalTestCase return array($name, $mustache); } + + public function testCustomDelimiters() + { + $mustache = new Mustache_Engine(array( + 'delimiters' => '[[ ]]', + 'partials' => array( + 'one' => '[[> two ]]', + 'two' => '[[ a ]]', + ), + )); + + $tpl = $mustache->loadTemplate('[[# a ]][[ b ]][[/a ]]'); + $this->assertEquals('c', $tpl->render(array('a' => true, 'b' => 'c'))); + + $tpl = $mustache->loadTemplate('[[> one ]]'); + $this->assertEquals('b', $tpl->render(array('a' => 'b'))); + } } class MustacheStub extends Mustache_Engine