Add a shortcut Mustache::render invocation.

Consistent with the old 1.x API.
This commit is contained in:
Justin Hileman
2012-03-04 21:42:20 -08:00
parent ee4c52de8f
commit 36e7247b1e
2 changed files with 49 additions and 0 deletions
+17
View File
@@ -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.
*
+32
View File
@@ -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.