diff --git a/Mustache.php b/Mustache.php index 079d951..0cef70c 100644 --- a/Mustache.php +++ b/Mustache.php @@ -61,6 +61,8 @@ class Mustache { self::PRAGMA_UNESCAPED ); + protected $_localPragmas; + /** * Mustache class constructor. * @@ -79,6 +81,28 @@ class Mustache { if ($view !== null) $this->_context = array($view); } + /** + * Mustache class clone method. + * + * A cloned Mustache instance should have pragmas, delimeters and root context + * reset to default values. + * + * @access public + * @return void + */ + public function __clone() { + $this->_otag = '{{'; + $this->_ctag = '}}'; + $this->_localPragmas = null; + + if ($keys = array_keys($this->_context)) { + $last = array_pop($keys); + if ($this->_context[$last] instanceof Mustache) { + $this->_context[$last] =& $this; + } + } + } + /** * Render the given template and view object. * @@ -101,7 +125,7 @@ class Mustache { $this->_context = array($this); } - $template = $this->_renderPragmas($template, $context); + $template = $this->_renderPragmas($template); return $this->_renderTemplate($template, $this->_context); } @@ -127,12 +151,11 @@ class Mustache { * * @access protected * @param string $template - * @param array &$context * @return string Rendered Mustache template. */ - protected function _renderTemplate($template, &$context) { - $template = $this->_renderSection($template, $context); - return $this->_renderTags($template, $context); + protected function _renderTemplate($template) { + $template = $this->_renderSection($template); + return $this->_renderTags($template); } /** @@ -140,12 +163,11 @@ class Mustache { * * @access protected * @param string $template - * @param array $context * @return string */ - protected function _renderSection($template, &$context) { - $otag = $this->_prepareRegEx($this->_otag); - $ctag = $this->_prepareRegEx($this->_ctag); + protected function _renderSection($template) { + $otag = preg_quote($this->_otag, '/'); + $ctag = preg_quote($this->_ctag, '/'); $regex = '/' . $otag . '(\\^|\\#)\\s*(.+?)\\s*' . $ctag . '\\s*([\\s\\S]+?)' . $otag . '\\/\\s*\\2\\s*' . $ctag . '\\s*/m'; $matches = array(); @@ -157,7 +179,7 @@ class Mustache { $content = $matches[3][0]; $replace = ''; - $val = $this->_getVariable($tag_name, $context); + $val = $this->_getVariable($tag_name); switch($type) { // inverted section case '^': @@ -180,19 +202,21 @@ class Mustache { } foreach ($val as $local_context) { + if ($iterator) { - $c1 = array($iterator => $local_context); - $c2 = $this->_getContext($context, $c1); - $replace .= $this->_renderTemplate($content, $c2); + $iterator_context = array($iterator => $local_context); + $this->_pushContext($iterator_context); } else { - $c = $this->_getContext($context, $local_context); - $replace .= $this->_renderTemplate($content, $c); + $this->_pushContext($local_context); } + $replace .= $this->_renderTemplate($content); + $this->_popContext(); } } else if ($val) { if (is_array($val) || is_object($val)) { - $c = $this->_getContext($context, $val); - $replace .= $this->_renderTemplate($content, $c); + $this->_pushContext($val); + $replace .= $this->_renderTemplate($content); + $this->_popContext(); } else { $replace .= $content; } @@ -211,17 +235,18 @@ class Mustache { * * @access protected * @param string $template - * @param array &$context * @return string */ - protected function _renderPragmas($template, &$context) { + protected function _renderPragmas($template) { + $this->_localPragmas = $this->_pragmas; + // no pragmas if (strpos($template, $this->_otag . '%') === false) { return $template; } - $otag = $this->_prepareRegEx($this->_otag); - $ctag = $this->_prepareRegEx($this->_ctag); + $otag = preg_quote($this->_otag, '/'); + $ctag = preg_quote($this->_ctag, '/'); $regex = '/' . $otag . '%\\s*([\\w_-]+)((?: [\\w]+=[\\w]+)*)\\s*' . $ctag . '\\n?/'; return preg_replace_callback($regex, array($this, '_renderPragma'), $template); } @@ -252,9 +277,9 @@ class Mustache { } if (empty($options)) { - $this->_pragmas[$pragma_name] = true; + $this->_localPragmas[$pragma_name] = true; } else { - $this->_pragmas[$pragma_name] = $options; + $this->_localPragmas[$pragma_name] = $options; } return ''; @@ -268,7 +293,7 @@ class Mustache { * @return bool */ protected function _hasPragma($pragma_name) { - if (array_key_exists($pragma_name, $this->_pragmas) && $this->_pragmas[$pragma_name]) { + if (array_key_exists($pragma_name, $this->_localPragmas) && $this->_localPragmas[$pragma_name]) { return true; } else { return false; @@ -288,7 +313,7 @@ class Mustache { throw new MustacheException('Unknown pragma: ' . $pragma_name, MustacheException::UNKNOWN_PRAGMA); } - return (is_array($this->_pragmas[$pragma_name])) ? $this->_pragmas[$pragma_name] : array(); + return (is_array($this->_localPragmas[$pragma_name])) ? $this->_localPragmas[$pragma_name] : array(); } @@ -310,16 +335,18 @@ class Mustache { * * @access protected * @param string $template - * @param array $context * @return void */ - protected function _renderTags($template, &$context) { + protected function _renderTags($template) { if (strpos($template, $this->_otag) === false) { return $template; } - $otag = $this->_prepareRegEx($this->_otag); - $ctag = $this->_prepareRegEx($this->_ctag); + $otag_orig = $this->_otag; + $ctag_orig = $this->_ctag; + + $otag = preg_quote($this->_otag, '/'); + $ctag = preg_quote($this->_ctag, '/'); $this->_tagRegEx = '/' . $otag . "([#\^\/=!>\\{&])?(.+?)\\1?" . $ctag . "+/"; @@ -332,10 +359,13 @@ class Mustache { $tag_name = trim($matches[2][0]); $html .= substr($template, 0, $offset); - $html .= $this->_renderTag($modifier, $tag_name, $context); + $html .= $this->_renderTag($modifier, $tag_name); $template = substr($template, $offset + strlen($tag)); } + $this->_otag = $otag_orig; + $this->_ctag = $ctag_orig; + return $html . $template; } @@ -348,11 +378,10 @@ class Mustache { * @access protected * @param string $modifier * @param string $tag_name - * @param array $context * @throws MustacheException Unmatched section tag encountered. * @return string */ - protected function _renderTag($modifier, $tag_name, &$context) { + protected function _renderTag($modifier, $tag_name) { switch ($modifier) { case '#': case '^': @@ -370,28 +399,28 @@ class Mustache { } break; case '=': - return $this->_changeDelimiter($tag_name, $context); + return $this->_changeDelimiter($tag_name); break; case '!': - return $this->_renderComment($tag_name, $context); + return $this->_renderComment($tag_name); break; case '>': - return $this->_renderPartial($tag_name, $context); + return $this->_renderPartial($tag_name); break; case '{': case '&': if ($this->_hasPragma(self::PRAGMA_UNESCAPED)) { - return $this->_renderEscaped($tag_name, $context); + return $this->_renderEscaped($tag_name); } else { - return $this->_renderUnescaped($tag_name, $context); + return $this->_renderUnescaped($tag_name); } break; case '': default: if ($this->_hasPragma(self::PRAGMA_UNESCAPED)) { - return $this->_renderUnescaped($tag_name, $context); + return $this->_renderUnescaped($tag_name); } else { - return $this->_renderEscaped($tag_name, $context); + return $this->_renderEscaped($tag_name); } break; } @@ -402,11 +431,10 @@ class Mustache { * * @access protected * @param string $tag_name - * @param array $context * @return string */ - protected function _renderEscaped($tag_name, &$context) { - return htmlentities($this->_getVariable($tag_name, $context), null, $this->_charset); + protected function _renderEscaped($tag_name) { + return htmlentities($this->_getVariable($tag_name), null, $this->_charset); } /** @@ -414,10 +442,9 @@ class Mustache { * * @access protected * @param string $tag_name - * @param array $context * @return string */ - protected function _renderComment($tag_name, &$context) { + protected function _renderComment($tag_name) { return ''; } @@ -426,11 +453,10 @@ class Mustache { * * @access protected * @param string $tag_name - * @param array $context * @return string */ - protected function _renderUnescaped($tag_name, &$context) { - return $this->_getVariable($tag_name, $context); + protected function _renderUnescaped($tag_name) { + return $this->_getVariable($tag_name); } /** @@ -438,14 +464,11 @@ class Mustache { * * @access protected * @param string $tag_name - * @param array $context * @return string */ - protected function _renderPartial($tag_name, &$context) { - $view = new self($this->_getPartial($tag_name), $this->_flattenContext($context), $this->_partials); - $view->_otag = $this->_otag; - $view->_ctag = $this->_ctag; - return $view->render(); + protected function _renderPartial($tag_name) { + $view = clone($this); + return $view->render($this->_getPartial($tag_name)); } /** @@ -454,77 +477,51 @@ class Mustache { * * @access protected * @param string $tag_name - * @param array $context * @return string */ - protected function _changeDelimiter($tag_name, &$context) { + protected function _changeDelimiter($tag_name) { $tags = explode(' ', $tag_name); $this->_otag = $tags[0]; $this->_ctag = $tags[1]; - $otag = $this->_prepareRegEx($this->_otag); - $ctag = $this->_prepareRegEx($this->_ctag); + $otag = preg_quote($this->_otag, '/'); + $ctag = preg_quote($this->_ctag, '/'); $this->_tagRegEx = '/' . $otag . "([#\^\/=!>\\{&])?(.+?)\\1?" . $ctag . "+/"; return ''; } - /** - * Prepare a new context reference array. - * - * This is used to create context arrays for iterable blocks. + * Push a local context onto the stack. * * @access protected - * @param array $context - * @param array $local_context - * @return array + * @param array &$local_context + * @return void */ - protected function _getContext(&$context, &$local_context) { - $ret = array(); - $ret[] =& $local_context; - foreach ($context as $view) { - $ret[] =& $view; + protected function _pushContext(&$local_context) { + $new = array(); + $new[] =& $local_context; + foreach (array_keys($this->_context) as $key) { + $new[] =& $this->_context[$key]; } - return $ret; + $this->_context = $new; } /** - * Prepare a new (flattened) context. - * - * This is used to create a view object or array for rendering partials. + * Remove the latest context from the stack. * * @access protected - * @param array &$context - * @return array - * @throws MustacheException + * @return void */ - protected function _flattenContext(&$context) { - $keys = array_keys($context); - $first = $context[$keys[0]]; + protected function _popContext() { + $new = array(); - if ($first instanceof Mustache) { - $ret = clone $first; - unset($keys[0]); - - foreach ($keys as $name) { - foreach ($context[$name] as $key => $val) { - $ret->$key =& $val; - } - } - } else if (is_array($first)) { - $ret = array(); - - foreach ($keys as $name) { - foreach ($context[$name] as $key => $val) { - $ret[$key] =& $val; - } - } - } else { - throw new MustacheException('Unknown root context type.'); + $keys = array_keys($this->_context); + array_shift($keys); + foreach ($keys as $key) { + $new[] =& $this->_context[$key]; } - - return $ret; + $this->_context = $new; } /** @@ -538,16 +535,15 @@ class Mustache { * * @access protected * @param string $tag_name - * @param array $context * @throws MustacheException Unknown variable name. * @return string */ - protected function _getVariable($tag_name, &$context) { + protected function _getVariable($tag_name) { if ($this->_hasPragma(self::PRAGMA_DOT_NOTATION) && $tag_name != '.') { $chunks = explode('.', $tag_name); $first = array_shift($chunks); - $ret = $this->_findVariableInContext($first, $context); + $ret = $this->_findVariableInContext($first, $this->_context); while ($next = array_shift($chunks)) { // Slice off a chunk of context for dot notation traversal. $c = array($ret); @@ -555,7 +551,7 @@ class Mustache { } return $ret; } else { - return $this->_findVariableInContext($tag_name, $context); + return $this->_findVariableInContext($tag_name, $this->_context); } } @@ -565,11 +561,11 @@ class Mustache { * * @access protected * @param string $tag_name - * @param array &$context + * @param array $context * @throws MustacheException Unknown variable name. * @return string */ - protected function _findVariableInContext($tag_name, &$context) { + protected function _findVariableInContext($tag_name, $context) { foreach ($context as $view) { if (is_object($view)) { if (isset($view->$tag_name)) { @@ -621,22 +617,6 @@ class Mustache { protected function _varIsIterable($var) { return is_object($var) || (is_array($var) && !array_diff_key($var, array_keys(array_keys($var)))); } - - /** - * Prepare a string to be used in a regular expression. - * - * @access protected - * @param string $str - * @return string - */ - protected function _prepareRegEx($str) { - $replace = array( - '\\' => '\\\\', '^' => '\^', '.' => '\.', '$' => '\$', '|' => '\|', '(' => '\(', - ')' => '\)', '[' => '\[', ']' => '\]', '*' => '\*', '+' => '\+', '?' => '\?', - '{' => '\{', '}' => '\}', ',' => '\,' - ); - return strtr($str, $replace); - } } diff --git a/README.markdown b/README.markdown index 4e843fd..3123c6d 100644 --- a/README.markdown +++ b/README.markdown @@ -81,8 +81,6 @@ And render it: Known Issues ------------ - * Pragmas don't un-apply... Instead of applying only to a specific template, pragmas are applied - to all subsequent templates and partials rendered by this Mustache instance. * Sections don't respect delimiter changes -- `delimiters` example currently fails with an "unclosed section" exception. * Dot notation and implicit iterators don't really play nice. A non-traversible local context is diff --git a/examples/grand_parent_context/GrandParentContext.php b/examples/grand_parent_context/GrandParentContext.php new file mode 100644 index 0000000..5a59ed9 --- /dev/null +++ b/examples/grand_parent_context/GrandParentContext.php @@ -0,0 +1,24 @@ +parent_contexts[] = array('parent_id' => 'parent1', 'child_contexts' => array( + array('child_id' => 'parent1-child1'), + array('child_id' => 'parent1-child2') + )); + + $parent2 = new stdClass(); + $parent2->parent_id = 'parent2'; + $parent2->child_contexts = array( + array('child_id' => 'parent2-child1'), + array('child_id' => 'parent2-child2') + ); + + $this->parent_contexts[] = $parent2; + } +} \ No newline at end of file diff --git a/examples/grand_parent_context/grand_parent_context.mustache b/examples/grand_parent_context/grand_parent_context.mustache new file mode 100644 index 0000000..e6c07a2 --- /dev/null +++ b/examples/grand_parent_context/grand_parent_context.mustache @@ -0,0 +1,10 @@ +{{grand_parent_id}} +{{#parent_contexts}} +{{grand_parent_id}} +{{parent_id}} +{{#child_contexts}} +{{grand_parent_id}} +{{parent_id}} +{{child_id}} +{{/child_contexts}} +{{/parent_contexts}} diff --git a/examples/grand_parent_context/grand_parent_context.txt b/examples/grand_parent_context/grand_parent_context.txt new file mode 100644 index 0000000..64996ad --- /dev/null +++ b/examples/grand_parent_context/grand_parent_context.txt @@ -0,0 +1,17 @@ +grand_parent1 +grand_parent1 +parent1 +grand_parent1 +parent1 +parent1-child1 +grand_parent1 +parent1 +parent1-child2 +grand_parent1 +parent2 +grand_parent1 +parent2 +parent2-child1 +grand_parent1 +parent2 +parent2-child2 diff --git a/examples/partials_with_view_class/PartialsWithViewClass.php b/examples/partials_with_view_class/PartialsWithViewClass.php new file mode 100644 index 0000000..56e0d86 --- /dev/null +++ b/examples/partials_with_view_class/PartialsWithViewClass.php @@ -0,0 +1,19 @@ +name = 'ilmich'; + $view->data = array( + array('name' => 'federica', 'age' => 27, 'gender' => 'female'), + array('name' => 'marco', 'age' => 32, 'gender' => 'male'), + ); + + $partials = array( + 'children' => "{{#data}}{{name}} - {{age}} - {{gender}}\n{{/data}}", + ); + + parent::__construct($template, $view, $partials); + } +} \ No newline at end of file diff --git a/examples/partials_with_view_class/partials_with_view_class.mustache b/examples/partials_with_view_class/partials_with_view_class.mustache new file mode 100644 index 0000000..037e1b3 --- /dev/null +++ b/examples/partials_with_view_class/partials_with_view_class.mustache @@ -0,0 +1,2 @@ +Children of {{name}}: +{{>children}} \ No newline at end of file diff --git a/examples/partials_with_view_class/partials_with_view_class.txt b/examples/partials_with_view_class/partials_with_view_class.txt new file mode 100644 index 0000000..d967e15 --- /dev/null +++ b/examples/partials_with_view_class/partials_with_view_class.txt @@ -0,0 +1,3 @@ +Children of ilmich: +federica - 27 - female +marco - 32 - male diff --git a/examples/pragma_unescaped/PragmaUnescaped.php b/examples/pragma_unescaped/PragmaUnescaped.php index 59681a8..b4e0e21 100644 --- a/examples/pragma_unescaped/PragmaUnescaped.php +++ b/examples/pragma_unescaped/PragmaUnescaped.php @@ -1,5 +1,5 @@ Shark'; + public $vs = 'Bear > Shark'; } \ No newline at end of file diff --git a/examples/pragmas_in_partials/PragmasInPartials.php b/examples/pragmas_in_partials/PragmasInPartials.php new file mode 100644 index 0000000..7458289 --- /dev/null +++ b/examples/pragmas_in_partials/PragmasInPartials.php @@ -0,0 +1,8 @@ +'; + protected $_partials = array( + 'dinosaur' => '{{say}}' + ); +} \ No newline at end of file diff --git a/examples/pragmas_in_partials/pragmas_in_partials.mustache b/examples/pragmas_in_partials/pragmas_in_partials.mustache new file mode 100644 index 0000000..abd6ef4 --- /dev/null +++ b/examples/pragmas_in_partials/pragmas_in_partials.mustache @@ -0,0 +1,3 @@ +{{%UNESCAPED}} +{{say}} +{{>dinosaur}} \ No newline at end of file diff --git a/examples/pragmas_in_partials/pragmas_in_partials.txt b/examples/pragmas_in_partials/pragmas_in_partials.txt new file mode 100644 index 0000000..c8e77e3 --- /dev/null +++ b/examples/pragmas_in_partials/pragmas_in_partials.txt @@ -0,0 +1,2 @@ +< RAWR!! > +< RAWR!! > \ No newline at end of file diff --git a/examples/recursive_partials/RecursivePartials.php b/examples/recursive_partials/RecursivePartials.php new file mode 100644 index 0000000..04e8af8 --- /dev/null +++ b/examples/recursive_partials/RecursivePartials.php @@ -0,0 +1,16 @@ + " > {{ name }}{{#child}}{{>child}}{{/child}}", + ); + + public $name = 'George'; + public $child = array( + 'name' => 'Dan', + 'child' => array( + 'name' => 'Justin', + 'child' => false, + ) + ); +} \ No newline at end of file diff --git a/examples/recursive_partials/recursive_partials.mustache b/examples/recursive_partials/recursive_partials.mustache new file mode 100644 index 0000000..0bc5d03 --- /dev/null +++ b/examples/recursive_partials/recursive_partials.mustache @@ -0,0 +1 @@ +{{name}}{{#child}}{{>child}}{{/child}} \ No newline at end of file diff --git a/examples/recursive_partials/recursive_partials.txt b/examples/recursive_partials/recursive_partials.txt new file mode 100644 index 0000000..681cdef --- /dev/null +++ b/examples/recursive_partials/recursive_partials.txt @@ -0,0 +1 @@ +George > Dan > Justin \ No newline at end of file diff --git a/test/MustachePragmaDotNotationTest.php b/test/MustachePragmaDotNotationTest.php index d6d31b7..488962b 100644 --- a/test/MustachePragmaDotNotationTest.php +++ b/test/MustachePragmaDotNotationTest.php @@ -1,7 +1,6 @@ assertEquals($m->render('{{%DOT-NOTATION}}{{one.one}}|{{one.two}}|{{one.three}}'), 'one-one|one-two|one-three'); } + public function testDotNotationContext() { + $data = array('parent' => array('items' => array( + array('item' => array('index' => 1)), + array('item' => array('index' => 2)), + array('item' => array('index' => 3)), + array('item' => array('index' => 4)), + array('item' => array('index' => 5)), + ))); + + $m = new Mustache('', $data); + $this->assertEquals('12345', $m->render('{{%DOT-NOTATION}}{{#parent}}{{#items}}{{item.index}}{{/items}}{{/parent}}')); + } + + public function testDotNotationSectionNames() { + $data = array('parent' => array('items' => array( + array('item' => array('index' => 1)), + array('item' => array('index' => 2)), + array('item' => array('index' => 3)), + array('item' => array('index' => 4)), + array('item' => array('index' => 5)), + ))); + + $m = new Mustache('', $data); + $this->assertEquals('.....', $m->render('{{%DOT-NOTATION}}{{#parent.items}}.{{/parent.items}}')); + $this->assertEquals('12345', $m->render('{{%DOT-NOTATION}}{{#parent.items}}{{item.index}}{{/parent.items}}')); + $this->assertEquals('12345', $m->render('{{%DOT-NOTATION}}{{#parent.items}}{{#item}}{{index}}{{/item}}{{/parent.items}}')); + } } \ No newline at end of file diff --git a/test/MustachePragmaTest.php b/test/MustachePragmaTest.php index 58f380e..086e458 100644 --- a/test/MustachePragmaTest.php +++ b/test/MustachePragmaTest.php @@ -1,7 +1,6 @@ assertEquals("1\n23", $m->render("1\n2{{%DOT-NOTATION}}\n3"), 'Wrong newline removed with pragma tag'); } + public function testPragmaReset() { + $m = new Mustache('', array('symbol' => '>>>')); + $this->assertEquals('>>>', $m->render('{{{symbol}}}')); + $this->assertEquals('>>>', $m->render('{{%UNESCAPED}}{{symbol}}')); + $this->assertEquals('>>>', $m->render('{{{symbol}}}')); + } } \ No newline at end of file diff --git a/test/MustachePragmaUnescapedTest.php b/test/MustachePragmaUnescapedTest.php index 559eeab..8331312 100644 --- a/test/MustachePragmaUnescapedTest.php +++ b/test/MustachePragmaUnescapedTest.php @@ -1,7 +1,6 @@ 'Bear > Shark')); $this->assertEquals('Bear > Shark', $m->render('{{%UNESCAPED}}{{title}}')); + $this->assertEquals('Bear > Shark', $m->render('{{title}}')); $this->assertEquals('Bear > Shark', $m->render('{{%UNESCAPED}}{{{title}}}')); + $this->assertEquals('Bear > Shark', $m->render('{{{title}}}')); } } \ No newline at end of file diff --git a/test/MustacheTest.php b/test/MustacheTest.php index 06e1874..c05b15b 100644 --- a/test/MustacheTest.php +++ b/test/MustacheTest.php @@ -1,7 +1,6 @@ assertEquals($first, $second); } + + /** + * Mustache should not use templates passed to the render() method for subsequent invocations. + * + * @access public + * @return void + */ + public function testResetTemplateForMultipleInvocations() { + $m = new Mustache('Sirve.'); + $this->assertEquals('No sirve.', $m->render('No sirve.')); + $this->assertEquals('Sirve.', $m->render()); + + $m2 = new Mustache(); + $this->assertEquals('No sirve.', $m2->render('No sirve.')); + $this->assertEquals('', $m2->render()); + } + + /** + * testClone function. + * + * @dataProvider getExamples + * @access public + * @return void + */ + public function test__clone($class, $template, $output) { + if ($class == 'Delimiters') { + $this->markTestSkipped("Known issue: sections don't respect delimiter changes"); + return; + } + + $m = new $class; + $n = clone $m; + + $n_output = $n->render($template); + + $o = clone $n; + + $this->assertEquals($m->render($template), $n_output); + $this->assertEquals($n_output, $o->render($template)); + + $this->assertNotSame($m, $n); + $this->assertNotSame($n, $o); + $this->assertNotSame($m, $o); + } + /** * Test everything in the `examples` directory. * @@ -178,6 +222,11 @@ class MustacheTest extends PHPUnit_Framework_TestCase { * @return void */ public function testExamples($class, $template, $output) { + if ($class == 'Delimiters') { + $this->markTestSkipped("Known issue: sections don't respect delimiter changes"); + return; + } + $m = new $class; $this->assertEquals($output, $m->render($template)); } @@ -239,4 +288,21 @@ class MustacheTest extends PHPUnit_Framework_TestCase { } return $ret; } + + public function testCrazyDelimiters() { + $m = new Mustache(null, array('result' => 'success')); + $this->assertEquals('success', $m->render('{{=[[ ]]=}}[[ result ]]')); + $this->assertEquals('success', $m->render('{{=(( ))=}}(( result ))')); + $this->assertEquals('success', $m->render('{{={$ $}=}}{$ result $}')); + $this->assertEquals('success', $m->render('{{=<.. ..>=}}<.. result ..>')); + $this->assertEquals('success', $m->render('{{=^^ ^^}}^^ result ^^')); + $this->assertEquals('success', $m->render('{{=// \\\\}}// result \\\\')); + } + + public function testResetDelimiters() { + $m = new Mustache(null, array('result' => 'success')); + $this->assertEquals('success', $m->render('{{=[[ ]]=}}[[ result ]]')); + $this->assertEquals('success', $m->render('{{=<< >>=}}<< result >>')); + $this->assertEquals('success', $m->render('{{=<% %>=}}<% result %>')); + } } \ No newline at end of file diff --git a/test/phpunit.xml b/test/phpunit.xml new file mode 100644 index 0000000..c70e6b0 --- /dev/null +++ b/test/phpunit.xml @@ -0,0 +1,6 @@ + + + + ./ + + \ No newline at end of file