From 2aaa820965fb12e7ee286575df93d33644580777 Mon Sep 17 00:00:00 2001 From: Justin Hileman Date: Wed, 21 Sep 2011 23:40:12 -0700 Subject: [PATCH] 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 022488b1..2bd9566b 100644 --- a/Mustache.php +++ b/Mustache.php @@ -819,7 +819,10 @@ protected function _findVariableInContext($tag_name, $context) { * @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 00000000..9c4b3864 --- /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 00000000..139594d5 --- /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 00000000..008c7142 --- /dev/null +++ b/test/fixtures/foo.mustache @@ -0,0 +1 @@ +{{ foo }} \ No newline at end of file