From 2aaa820965fb12e7ee286575df93d33644580777 Mon Sep 17 00:00:00 2001 From: Justin Hileman Date: Wed, 21 Sep 2011 23:40:12 -0700 Subject: [PATCH 1/3] Add a 'MustacheLoader' filesystem loader. --- Mustache.php | 5 ++- MustacheLoader.php | 85 ++++++++++++++++++++++++++++++++++++++ test/fixtures/bar.mustache | 1 + test/fixtures/foo.mustache | 1 + 4 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 MustacheLoader.php create mode 100644 test/fixtures/bar.mustache create mode 100644 test/fixtures/foo.mustache diff --git a/Mustache.php b/Mustache.php index 022488b..2bd9566 100644 --- a/Mustache.php +++ b/Mustache.php @@ -819,7 +819,10 @@ class Mustache { * @return string */ protected function _getPartial($tag_name) { - if (is_array($this->_partials) && isset($this->_partials[$tag_name])) { + if ( + (is_array($this->_partials) || $this->_partials instanceof ArrayAccess) + && isset($this->_partials[$tag_name]) + ) { return $this->_partials[$tag_name]; } diff --git a/MustacheLoader.php b/MustacheLoader.php new file mode 100644 index 0000000..9c4b386 --- /dev/null +++ b/MustacheLoader.php @@ -0,0 +1,85 @@ +baseDir = $baseDir; + $this->extension = $extension; + } + + /** + * @param string $offset Name of partial + * @return boolean + */ + public function offsetExists($offset) { + return (isset($this->partialsCache[$offset]) || file_exists($this->pathName($offset))); + } + + /** + * @throws InvalidArgumentException if the given partial doesn't exist + * @param string $offset Name of partial + * @return string Partial template contents + */ + public function offsetGet($offset) { + if (!$this->offsetExists($offset)) { + throw new InvalidArgumentException('Partial does not exist: ' . $offset); + } + + if (!isset($this->partialsCache[$offset])) { + $this->partialsCache[$offset] = file_get_contents($this->pathName($offset)); + } + + return $this->partialsCache[$offset]; + } + + /** + * MustacheLoader is an immutable filesystem loader. offsetSet throws a LogicException if called. + * + * @throws LogicException + * @return void + */ + public function offsetSet($offset, $value) { + throw new LogicException('Unable to set offset: MustacheLoader is an immutable ArrayAccess object.'); + } + + /** + * MustacheLoader is an immutable filesystem loader. offsetUnset throws a LogicException if called. + * + * @throws LogicException + * @return void + */ + public function offsetUnset($offset) { + throw new LogicException('Unable to unset offset: MustacheLoader is an immutable ArrayAccess object.'); + } + + /** + * An internal helper for generating path names. + * + * @param string $file Partial name + * @return string File path + */ + protected function pathName($file) { + return $this->baseDir . '/' . $file . '.' . $this->extension; + } +} diff --git a/test/fixtures/bar.mustache b/test/fixtures/bar.mustache new file mode 100644 index 0000000..139594d --- /dev/null +++ b/test/fixtures/bar.mustache @@ -0,0 +1 @@ +{{# truthy }}{{ bar }}{{/ truthy }} \ No newline at end of file diff --git a/test/fixtures/foo.mustache b/test/fixtures/foo.mustache new file mode 100644 index 0000000..008c714 --- /dev/null +++ b/test/fixtures/foo.mustache @@ -0,0 +1 @@ +{{ foo }} \ No newline at end of file From 5bc11ef1832f6e0a1ad2dd47207970fcc63ea3e3 Mon Sep 17 00:00:00 2001 From: Justin Hileman Date: Thu, 22 Sep 2011 09:26:52 -0700 Subject: [PATCH 2/3] Test for the mustache loader. --- test/MustacheLoaderTest.php | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 test/MustacheLoaderTest.php diff --git a/test/MustacheLoaderTest.php b/test/MustacheLoaderTest.php new file mode 100644 index 0000000..ebb5745 --- /dev/null +++ b/test/MustacheLoaderTest.php @@ -0,0 +1,30 @@ +assertEquals(file_get_contents(dirname(__FILE__).'/fixtures/foo.mustache'), $loader['foo']); + $this->assertEquals(file_get_contents(dirname(__FILE__).'/fixtures/bar.mustache'), $loader['bar']); + } + + public function testMustacheUsesFilesystemLoader() { + $template = '{{> foo }}{{> bar }}'; + $data = array( + 'truthy' => true, + 'foo' => 'FOO', + 'bar' => 'BAR' + ); + $output = '{{ foo }}{{ bar }}'; + $m = new Mustache(); + $partials = new MustacheLoader(dirname(__FILE__).'/fixtures'); + $m->render($template, $data, $partials); + $this->assertEquals($output, $output); + } +} \ No newline at end of file From 38c5e9e8ea5ae8f25e6063c2fe843b103fe05c03 Mon Sep 17 00:00:00 2001 From: Justin Hileman Date: Sun, 8 Jan 2012 16:19:58 -0800 Subject: [PATCH 3/3] Fix MustacheLoader test, add a second loader test. --- test/MustacheLoaderTest.php | 42 +++++++++++++++++++++++++++++++------ 1 file changed, 36 insertions(+), 6 deletions(-) diff --git a/test/MustacheLoaderTest.php b/test/MustacheLoaderTest.php index ebb5745..2674a0f 100644 --- a/test/MustacheLoaderTest.php +++ b/test/MustacheLoaderTest.php @@ -15,16 +15,46 @@ class MustacheLoaderTest extends PHPUnit_Framework_TestCase { } public function testMustacheUsesFilesystemLoader() { - $template = '{{> foo }}{{> bar }}'; + $template = '{{> foo }} {{> bar }}'; $data = array( 'truthy' => true, 'foo' => 'FOO', - 'bar' => 'BAR' + 'bar' => 'BAR', ); - $output = '{{ foo }}{{ bar }}'; + $output = 'FOO BAR'; $m = new Mustache(); $partials = new MustacheLoader(dirname(__FILE__).'/fixtures'); - $m->render($template, $data, $partials); - $this->assertEquals($output, $output); + $this->assertEquals($output, $m->render($template, $data, $partials)); } -} \ No newline at end of file + + public function testMustacheUsesDifferentLoadersToo() { + $template = '{{> foo }} {{> bar }}'; + $data = array( + 'truthy' => true, + 'foo' => 'FOO', + 'bar' => 'BAR', + ); + $output = 'FOO BAR'; + $m = new Mustache(); + $partials = new DifferentMustacheLoader(); + $this->assertEquals($output, $m->render($template, $data, $partials)); + } +} + +class DifferentMustacheLoader implements ArrayAccess { + protected $partials = array( + 'foo' => '{{ foo }}', + 'bar' => '{{# truthy }}{{ bar }}{{/ truthy }}', + ); + + public function offsetExists($offset) { + return isset($this->partials[$offset]); + } + + public function offsetGet($offset) { + return $this->partials[$offset]; + } + + public function offsetSet($offset, $value) {} + public function offsetUnset($offset) {} +}