Stop passing context arrays around like a madman.

This commit is contained in:
Justin Hileman
2010-05-22 19:24:49 -04:00
parent eeb5b245d1
commit 7f5f7a73fc
+86 -58
View File
@@ -59,6 +59,8 @@ class Mustache {
self::PRAGMA_UNESCAPED self::PRAGMA_UNESCAPED
); );
protected $_localPragmas;
/** /**
* Mustache class constructor. * Mustache class constructor.
* *
@@ -77,6 +79,27 @@ class Mustache {
if ($view !== null) $this->_context = array($view); if ($view !== null) $this->_context = array($view);
} }
/**
* Mustache class clone method.
*
* A cloned Mustache instance should have pragmas, delimeters and root context
* reset to default values.
*
* @access public
* @return void
*/
public function __clone() {
$this->_otag = '{{';
$this->_ctag = '}}';
$this->_localPragmas = null;
if ($keys = array_keys($this->_context)) {
if ($this->_context[$keys[0]] instanceof Mustache) {
$this->_context[$keys[0]] =& $this;
}
}
}
/** /**
* Render the given template and view object. * Render the given template and view object.
* *
@@ -124,13 +147,12 @@ class Mustache {
* *
* @access protected * @access protected
* @param string $template * @param string $template
* @param array &$context
* @return string Rendered Mustache template. * @return string Rendered Mustache template.
*/ */
protected function _renderTemplate($template, &$context) { protected function _renderTemplate($template) {
$template = $this->_renderPragmas($template, $context); $template = $this->_renderPragmas($template);
$template = $this->_renderSection($template, $context); $template = $this->_renderSection($template);
return $this->_renderTags($template, $context); return $this->_renderTags($template);
} }
/** /**
@@ -138,10 +160,9 @@ class Mustache {
* *
* @access protected * @access protected
* @param string $template * @param string $template
* @param array $context
* @return string * @return string
*/ */
protected function _renderSection($template, &$context) { protected function _renderSection($template) {
$otag = $this->_prepareRegEx($this->_otag); $otag = $this->_prepareRegEx($this->_otag);
$ctag = $this->_prepareRegEx($this->_ctag); $ctag = $this->_prepareRegEx($this->_ctag);
$regex = '/' . $otag . '(\\^|\\#)\\s*(.+?)\\s*' . $ctag . '\\s*([\\s\\S]+?)' . $otag . '\\/\\s*\\2\\s*' . $ctag . '\\s*/m'; $regex = '/' . $otag . '(\\^|\\#)\\s*(.+?)\\s*' . $ctag . '\\s*([\\s\\S]+?)' . $otag . '\\/\\s*\\2\\s*' . $ctag . '\\s*/m';
@@ -155,7 +176,7 @@ class Mustache {
$content = $matches[3][0]; $content = $matches[3][0];
$replace = ''; $replace = '';
$val = $this->_getVariable($tag_name, $context); $val = $this->_getVariable($tag_name);
switch($type) { switch($type) {
// inverted section // inverted section
case '^': case '^':
@@ -168,13 +189,15 @@ class Mustache {
case '#': case '#':
if ($this->_varIsIterable($val)) { if ($this->_varIsIterable($val)) {
foreach ($val as $local_context) { foreach ($val as $local_context) {
$c = $this->_getContext($context, $local_context); $this->_pushContext($local_context);
$replace .= $this->_renderTemplate($content, $c); $replace .= $this->_renderTemplate($content);
$this->_popContext();
} }
} else if ($val) { } else if ($val) {
if (is_array($val) || is_object($val)) { if (is_array($val) || is_object($val)) {
$c = $this->_getContext($context, $val); $this->_pushContext($val);
$replace .= $this->_renderTemplate($content, $c); $replace .= $this->_renderTemplate($content);
$this->_popContext();
} else { } else {
$replace .= $content; $replace .= $content;
} }
@@ -193,10 +216,11 @@ class Mustache {
* *
* @access protected * @access protected
* @param string $template * @param string $template
* @param array &$context
* @return string * @return string
*/ */
protected function _renderPragmas($template, &$context) { protected function _renderPragmas($template) {
$this->_localPragmas = $this->_pragmas;
// no pragmas // no pragmas
if (strpos($template, $this->_otag . '%') === false) { if (strpos($template, $this->_otag . '%') === false) {
return $template; return $template;
@@ -234,9 +258,9 @@ class Mustache {
} }
if (empty($options)) { if (empty($options)) {
$this->_pragmas[$pragma_name] = true; $this->_localPragmas[$pragma_name] = true;
} else { } else {
$this->_pragmas[$pragma_name] = $options; $this->_localPragmas[$pragma_name] = $options;
} }
return ''; return '';
@@ -250,7 +274,7 @@ class Mustache {
* @return bool * @return bool
*/ */
protected function _hasPragma($pragma_name) { protected function _hasPragma($pragma_name) {
if (array_key_exists($pragma_name, $this->_pragmas) && $this->_pragmas[$pragma_name]) { if (array_key_exists($pragma_name, $this->_localPragmas) && $this->_localPragmas[$pragma_name]) {
return true; return true;
} else { } else {
return false; return false;
@@ -270,7 +294,7 @@ class Mustache {
throw new MustacheException('Unknown pragma: ' . $pragma_name, MustacheException::UNKNOWN_PRAGMA); throw new MustacheException('Unknown pragma: ' . $pragma_name, MustacheException::UNKNOWN_PRAGMA);
} }
return $this->_pragmas[$pragma_name]; return $this->_localPragmas[$pragma_name];
} }
@@ -292,10 +316,9 @@ class Mustache {
* *
* @access protected * @access protected
* @param string $template * @param string $template
* @param array $context
* @return void * @return void
*/ */
protected function _renderTags($template, &$context) { protected function _renderTags($template) {
if (strpos($template, $this->_otag) === false) { if (strpos($template, $this->_otag) === false) {
return $template; return $template;
} }
@@ -314,7 +337,7 @@ class Mustache {
$tag_name = trim($matches[2][0]); $tag_name = trim($matches[2][0]);
$html .= substr($template, 0, $offset); $html .= substr($template, 0, $offset);
$html .= $this->_renderTag($modifier, $tag_name, $context); $html .= $this->_renderTag($modifier, $tag_name);
$template = substr($template, $offset + strlen($tag)); $template = substr($template, $offset + strlen($tag));
} }
@@ -330,11 +353,10 @@ class Mustache {
* @access protected * @access protected
* @param string $modifier * @param string $modifier
* @param string $tag_name * @param string $tag_name
* @param array $context
* @throws MustacheException Unmatched section tag encountered. * @throws MustacheException Unmatched section tag encountered.
* @return string * @return string
*/ */
protected function _renderTag($modifier, $tag_name, &$context) { protected function _renderTag($modifier, $tag_name) {
switch ($modifier) { switch ($modifier) {
case '#': case '#':
case '^': case '^':
@@ -352,28 +374,28 @@ class Mustache {
} }
break; break;
case '=': case '=':
return $this->_changeDelimiter($tag_name, $context); return $this->_changeDelimiter($tag_name);
break; break;
case '!': case '!':
return $this->_renderComment($tag_name, $context); return $this->_renderComment($tag_name);
break; break;
case '>': case '>':
return $this->_renderPartial($tag_name, $context); return $this->_renderPartial($tag_name);
break; break;
case '{': case '{':
case '&': case '&':
if ($this->_hasPragma(self::PRAGMA_UNESCAPED)) { if ($this->_hasPragma(self::PRAGMA_UNESCAPED)) {
return $this->_renderEscaped($tag_name, $context); return $this->_renderEscaped($tag_name);
} else { } else {
return $this->_renderUnescaped($tag_name, $context); return $this->_renderUnescaped($tag_name);
} }
break; break;
case '': case '':
default: default:
if ($this->_hasPragma(self::PRAGMA_UNESCAPED)) { if ($this->_hasPragma(self::PRAGMA_UNESCAPED)) {
return $this->_renderUnescaped($tag_name, $context); return $this->_renderUnescaped($tag_name);
} else { } else {
return $this->_renderEscaped($tag_name, $context); return $this->_renderEscaped($tag_name);
} }
break; break;
} }
@@ -384,11 +406,10 @@ class Mustache {
* *
* @access protected * @access protected
* @param string $tag_name * @param string $tag_name
* @param array $context
* @return string * @return string
*/ */
protected function _renderEscaped($tag_name, &$context) { protected function _renderEscaped($tag_name) {
return htmlentities($this->_getVariable($tag_name, $context), null, $this->_charset); return htmlentities($this->_getVariable($tag_name), null, $this->_charset);
} }
/** /**
@@ -396,10 +417,9 @@ class Mustache {
* *
* @access protected * @access protected
* @param string $tag_name * @param string $tag_name
* @param array $context
* @return string * @return string
*/ */
protected function _renderComment($tag_name, &$context) { protected function _renderComment($tag_name) {
return ''; return '';
} }
@@ -408,11 +428,10 @@ class Mustache {
* *
* @access protected * @access protected
* @param string $tag_name * @param string $tag_name
* @param array $context
* @return string * @return string
*/ */
protected function _renderUnescaped($tag_name, &$context) { protected function _renderUnescaped($tag_name) {
return $this->_getVariable($tag_name, $context); return $this->_getVariable($tag_name);
} }
/** /**
@@ -420,13 +439,10 @@ class Mustache {
* *
* @access protected * @access protected
* @param string $tag_name * @param string $tag_name
* @param array $context
* @return string * @return string
*/ */
protected function _renderPartial($tag_name, &$context) { protected function _renderPartial($tag_name) {
$view = clone($this); $view = clone($this);
$view->_otag = '{{';
$view->_ctag = '}}';
return $view->render($this->_getPartial($tag_name)); return $view->render($this->_getPartial($tag_name));
} }
@@ -436,10 +452,9 @@ class Mustache {
* *
* @access protected * @access protected
* @param string $tag_name * @param string $tag_name
* @param array $context
* @return string * @return string
*/ */
protected function _changeDelimiter($tag_name, &$context) { protected function _changeDelimiter($tag_name) {
$tags = explode(' ', $tag_name); $tags = explode(' ', $tag_name);
$this->_otag = $tags[0]; $this->_otag = $tags[0];
$this->_ctag = $tags[1]; $this->_ctag = $tags[1];
@@ -451,22 +466,36 @@ class Mustache {
} }
/** /**
* Prepare a new context reference array. * Push a local context onto the stack.
*
* This is used to create context arrays for iterable blocks.
* *
* @access protected * @access protected
* @param array $context
* @param array $local_context * @param array $local_context
* @return array * @return array
*/ */
protected function _getContext(&$context, &$local_context) { protected function _pushContext(&$local_context) {
$ret = array(); $new = array();
$ret[] =& $local_context; $new[] =& $local_context;
foreach ($context as $view) { foreach ($this->_context as $view) {
$ret[] =& $view; $new[] =& $view;
} }
return $ret; $this->_context = $new;
}
/**
* Remove the latest context from the stack.
*
* @access protected
* @return void
*/
protected function _popContext() {
$new = array();
array_shift($this->_context);
foreach ($this->_context as $view) {
$new[] =& $view;
}
$this->_context = $new;
} }
/** /**
@@ -480,16 +509,15 @@ class Mustache {
* *
* @access protected * @access protected
* @param string $tag_name * @param string $tag_name
* @param array $context
* @throws MustacheException Unknown variable name. * @throws MustacheException Unknown variable name.
* @return string * @return string
*/ */
protected function _getVariable($tag_name, &$context) { protected function _getVariable($tag_name) {
if ($this->_hasPragma(self::PRAGMA_DOT_NOTATION)) { if ($this->_hasPragma(self::PRAGMA_DOT_NOTATION)) {
$chunks = explode('.', $tag_name); $chunks = explode('.', $tag_name);
$first = array_shift($chunks); $first = array_shift($chunks);
$ret = $this->_findVariableInContext($first, $context); $ret = $this->_findVariableInContext($first, $this->_context);
while ($next = array_shift($chunks)) { while ($next = array_shift($chunks)) {
// Slice off a chunk of context for dot notation traversal. // Slice off a chunk of context for dot notation traversal.
$c = array($ret); $c = array($ret);
@@ -497,7 +525,7 @@ class Mustache {
} }
return $ret; return $ret;
} else { } else {
return $this->_findVariableInContext($tag_name, $context); return $this->_findVariableInContext($tag_name, $this->_context);
} }
} }