Эх сурвалжийг харах

Add a shortcut Mustache::render invocation.

Consistent with the old 1.x API.
Justin Hileman 13 жил өмнө
parent
commit
36e7247b1e

+ 17 - 0
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.
 	 * Get the current Mustache character set.
 	 *
 	 *

+ 32 - 0
test/Mustache/Test/MustacheTest.php

@@ -53,6 +53,27 @@ class MustacheTest extends \PHPUnit_Framework_TestCase {
 		$this->assertEquals('ISO-8859-1', $mustache->getCharset());
 		$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() {
 	public function testSettingServices() {
 		$loader    = new StringLoader;
 		$loader    = new StringLoader;
 		$tokenizer = new Tokenizer;
 		$tokenizer = new Tokenizer;
@@ -122,6 +143,7 @@ class MustacheTest extends \PHPUnit_Framework_TestCase {
 
 
 		$mustache->setPartials(array('foo' => '{{ foo }}'));
 		$mustache->setPartials(array('foo' => '{{ foo }}'));
 	}
 	}
+
 	private static function rmdir($path) {
 	private static function rmdir($path) {
 		$path = rtrim($path, '/').'/';
 		$path = rtrim($path, '/').'/';
 		$handle = opendir($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.
 // It's prob'ly best if you ignore this bit.