From 6a65bcda03039e90602b95d74e957faf06ece8ac Mon Sep 17 00:00:00 2001 From: Justin Hileman Date: Wed, 15 Dec 2010 10:47:28 -0500 Subject: [PATCH] Update (per the lambdas spec) to allow lambdas as "higher-order variables". --- Mustache.php | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/Mustache.php b/Mustache.php index 153572d..c0ea3a9 100644 --- a/Mustache.php +++ b/Mustache.php @@ -106,6 +106,11 @@ class Mustache { protected $_localPragmas = array(); + /** + * Interpolated lambdas should be called only once... store 'em for later here + */ + protected $_interpolatedLambdas = array(); + /** * Mustache class constructor. * @@ -226,7 +231,7 @@ class Mustache { case '#': // higher order sections - if ($this->_sectionIsCallable($val)) { + if ($this->_varIsCallable($val)) { $content = call_user_func($val, $content); $replace .= $this->_renderTemplate($content); } else if ($this->_varIsIterable($val)) { @@ -586,7 +591,7 @@ class Mustache { * @return string */ protected function _renderEscaped($tag_name) { - return htmlentities($this->_getVariable($tag_name), ENT_COMPAT, $this->_charset); + return htmlentities($this->_renderUnescaped($tag_name), ENT_COMPAT, $this->_charset); } /** @@ -608,7 +613,17 @@ class Mustache { * @return string */ protected function _renderUnescaped($tag_name) { - return $this->_getVariable($tag_name); + $val = $this->_getVariable($tag_name); + + if ($this->_varIsCallable($val)) { + $key = is_object($val) ? spl_object_hash($val) : serialize($val); + if (!isset($this->_interpolatedLambdas[$key])) { + $this->_interpolatedLambdas[$key] = call_user_func($val); + } + return $this->_renderTemplate($this->_interpolatedLambdas[$key]); + } + + return $val; } /** @@ -783,7 +798,7 @@ class Mustache { * @param mixed $var * @return bool */ - protected function _sectionIsCallable($var) { + protected function _varIsCallable($var) { if (is_string($var) && (strpos($var, '::') == false)) { return false; }