Add a shortcut Mustache::render invocation.
Consistent with the old 1.x API.
This commit is contained in:
@@ -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.
|
||||
*
|
||||
|
||||
@@ -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.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user