diff --git a/src/Mustache/Mustache.php b/src/Mustache/Mustache.php index d996ef3..8975907 100644 --- a/src/Mustache/Mustache.php +++ b/src/Mustache/Mustache.php @@ -95,6 +95,23 @@ class Mustache { } } + /** + * Shortcut 'render' invocation. + * + * Equivalent to calling `$mustache->loadTemplate($template)->render($data);` + * + * @see \Mustache\Mustache::loadTemplate + * @see \Mustache\Template::render + * + * @param string $template + * @param mixed $data + * + * @return string Rendered template + */ + public function render($template, $data) { + return $this->loadTemplate($template)->render($data); + } + /** * Get the current Mustache character set. * diff --git a/test/Mustache/Test/MustacheTest.php b/test/Mustache/Test/MustacheTest.php index 4ed9115..88f9a03 100644 --- a/test/Mustache/Test/MustacheTest.php +++ b/test/Mustache/Test/MustacheTest.php @@ -53,6 +53,27 @@ class MustacheTest extends \PHPUnit_Framework_TestCase { $this->assertEquals('ISO-8859-1', $mustache->getCharset()); } + public function testRender() { + $source = '{{ foo }}'; + $data = array('bar' => 'baz'); + $output = 'TEH OUTPUT'; + + $template = $this->getMockBuilder('Mustache\Template') + ->disableOriginalConstructor() + ->getMock(); + + $mustache = new MustacheStub; + $mustache->template = $template; + + $template->expects($this->once()) + ->method('render') + ->with($data) + ->will($this->returnValue($output)); + + $this->assertEquals($output, $mustache->render($source, $data)); + $this->assertEquals($source, $mustache->source); + } + public function testSettingServices() { $loader = new StringLoader; $tokenizer = new Tokenizer; @@ -122,6 +143,7 @@ class MustacheTest extends \PHPUnit_Framework_TestCase { $mustache->setPartials(array('foo' => '{{ foo }}')); } + private static function rmdir($path) { $path = rtrim($path, '/').'/'; $handle = opendir($path); @@ -143,6 +165,16 @@ class MustacheTest extends \PHPUnit_Framework_TestCase { } } +class MustacheStub extends Mustache { + public $source; + public $template; + public function loadTemplate($source) { + $this->source = $source; + + return $this->template; + } +} + // It's prob'ly best if you ignore this bit.