mustache = new Mustache_Engine(array( 'pragmas' => array(Mustache_Engine::PRAGMA_BLOCKS), )); } public function getIllegalInheritanceExamples() { return array( array( array( 'foo' => '{{$baz}}default content{{/baz}}', ), array( 'bar' => 'set by user', ), '{{< foo }}{{# bar }}{{$ baz }}{{/ baz }}{{/ bar }}{{/ foo }}', ), array( array( 'foo' => '{{$baz}}default content{{/baz}}', ), array( ), '{{ '{{$baz}}default content{{/baz}}', 'qux' => 'I am a partial', ), array( ), '{{qux}}{{$baz}}set by template{{/baz}}{{/foo}}', ), array( array( 'foo' => '{{$baz}}default content{{/baz}}', ), array(), '{{=}}<%={{ }}=%>{{/foo}}', ), ); } public function getLegalInheritanceExamples() { return array( array( array( 'foo' => '{{$baz}}default content{{/baz}}', ), array( 'bar' => 'set by user', ), '{{ '{{$baz}}default content{{/baz}}', ), array( ), '{{ '{{$baz}}defualt content{{/baz}}', ), array(), '{{mustache->loadTemplate('{{$title}}Default title{{/title}}'); $data = array(); $this->assertEquals('Default title', $tpl->render($data)); } public function testDefaultContentRendersVariables() { $tpl = $this->mustache->loadTemplate('{{$foo}}default {{bar}} content{{/foo}}'); $data = array( 'bar' => 'baz', ); $this->assertEquals('default baz content', $tpl->render($data)); } public function testDefaultContentRendersTripleMustacheVariables() { $tpl = $this->mustache->loadTemplate('{{$foo}}default {{{bar}}} content{{/foo}}'); $data = array( 'bar' => '', ); $this->assertEquals('default content', $tpl->render($data)); } public function testDefaultContentRendersSections() { $tpl = $this->mustache->loadTemplate( '{{$foo}}default {{#bar}}{{baz}}{{/bar}} content{{/foo}}' ); $data = array( 'bar' => array('baz' => 'qux'), ); $this->assertEquals('default qux content', $tpl->render($data)); } public function testDefaultContentRendersNegativeSections() { $tpl = $this->mustache->loadTemplate( '{{$foo}}default {{^bar}}{{baz}}{{/bar}} content{{/foo}}' ); $data = array( 'foo' => array('bar' => 'qux'), 'baz' => 'three', ); $this->assertEquals('default three content', $tpl->render($data)); } public function testMustacheInjectionInDefaultContent() { $tpl = $this->mustache->loadTemplate( '{{$foo}}default {{#bar}}{{baz}}{{/bar}} content{{/foo}}' ); $data = array( 'bar' => array('baz' => '{{qux}}'), ); $this->assertEquals('default {{qux}} content', $tpl->render($data)); } public function testDefaultContentRenderedInsideIncludedTemplates() { $partials = array( 'include' => '{{$foo}}default content{{/foo}}', ); $this->mustache->setPartials($partials); $tpl = $this->mustache->loadTemplate( '{{assertEquals('default content', $tpl->render($data)); } public function testOverriddenContent() { $partials = array( 'super' => '...{{$title}}Default title{{/title}}...', ); $this->mustache->setPartials($partials); $tpl = $this->mustache->loadTemplate( '{{assertEquals('...sub template title...', $tpl->render($data)); } public function testOverriddenPartial() { $partials = array( 'partial' => '|{{$stuff}}...{{/stuff}}{{$default}} default{{/default}}|', ); $this->mustache->setPartials($partials); $tpl = $this->mustache->loadTemplate( 'test {{assertEquals('test |override1 default| |override2 default|', $tpl->render($data)); } public function testDataDoesNotOverrideBlock() { $partials = array( 'include' => '{{$var}}var in include{{/var}}', ); $this->mustache->setPartials($partials); $tpl = $this->mustache->loadTemplate( '{{ 'var in data', ); $this->assertEquals('var in template', $tpl->render($data)); } public function testDataDoesNotOverrideDefaultBlockValue() { $partials = array( 'include' => '{{$var}}var in include{{/var}}', ); $this->mustache->setPartials($partials); $tpl = $this->mustache->loadTemplate( '{{ 'var in data', ); $this->assertEquals('var in include', $tpl->render($data)); } public function testOverridePartialWithNewlines() { $partials = array( 'partial' => '{{$ballmer}}peaking{{/ballmer}}', ); $this->mustache->setPartials($partials); $tpl = $this->mustache->loadTemplate( "{{assertEquals("peaked\n\n:(\n", $tpl->render($data)); } public function testInheritIndentationWhenOverridingAPartial() { $partials = array( 'partial' => 'stop: {{$nineties}}collaborate and listen{{/nineties}}', ); $this->mustache->setPartials($partials); $tpl = $this->mustache->loadTemplate( '{{assertEquals( 'stop: hammer time', $tpl->render($data) ); } public function testOverrideOneSubstitutionButNotTheOther() { $partials = array( 'partial' => '{{$stuff}}default one{{/stuff}}, {{$stuff2}}default two{{/stuff2}}', ); $this->mustache->setPartials($partials); $tpl = $this->mustache->loadTemplate( '{{assertEquals('default one, override two', $tpl->render($data)); } public function testSuperTemplatesWithNoParameters() { $partials = array( 'include' => '{{$foo}}default content{{/foo}}', ); $this->mustache->setPartials($partials); $tpl = $this->mustache->loadTemplate( '{{>include}}|{{assertEquals('default content|default content', $tpl->render($data)); } public function testRecursionInInheritedTemplates() { $partials = array( 'include' => '{{$foo}}default content{{/foo}} {{$bar}}{{ '{{$foo}}include2 default content{{/foo}} {{mustache->setPartials($partials); $tpl = $this->mustache->loadTemplate( '{{assertEquals('override override override don\'t recurse', $tpl->render($data)); } public function testTopLevelSubstitutionsTakePrecedenceInMultilevelInheritance() { $partials = array( 'parent' => '{{ '{{ '{{$a}}g{{/a}}', ); $this->mustache->setPartials($partials); $tpl = $this->mustache->loadTemplate( '{{assertEquals('c', $tpl->render($data)); } public function testMultiLevelInheritanceNoSubChild() { $partials = array( 'parent' => '{{ '{{ '{{$a}}g{{/a}}', ); $this->mustache->setPartials($partials); $tpl = $this->mustache->loadTemplate( '{{assertEquals('p', $tpl->render($data)); } public function testIgnoreTextInsideSuperTemplatesButParseArgs() { $partials = array( 'include' => '{{$foo}}default content{{/foo}}', ); $this->mustache->setPartials($partials); $tpl = $this->mustache->loadTemplate( '{{assertEquals('hmm', $tpl->render($data)); } public function testIgnoreTextInsideSuperTemplates() { $partials = array( 'include' => '{{$foo}}default content{{/foo}}', ); $this->mustache->setPartials($partials); $tpl = $this->mustache->loadTemplate( '{{assertEquals('default content', $tpl->render($data)); } /** * @dataProvider getIllegalInheritanceExamples * @expectedException Mustache_Exception_SyntaxException * @expectedExceptionMessage Illegal content in < parent tag */ public function testIllegalInheritanceExamples($partials, $data, $template) { $this->mustache->setPartials($partials); $tpl = $this->mustache->loadTemplate($template); $tpl->render($data); } /** * @dataProvider getLegalInheritanceExamples */ public function testLegalInheritanceExamples($partials, $data, $template, $expect) { $this->mustache->setPartials($partials); $tpl = $this->mustache->loadTemplate($template); $this->assertSame($expect, $tpl->render($data)); } }