upgrading to mustache 0.2.4
This commit is contained in:
parent
d95bff3116
commit
c22610ebef
222
vendor/Mustache.php
vendored
222
vendor/Mustache.php
vendored
|
@ -43,7 +43,7 @@ class Mustache {
|
|||
* Pragmas apply only to the current template. Partials, even those included after the
|
||||
* {{%UNESCAPED}} call, will need their own pragma declaration.
|
||||
*
|
||||
* his may be useful in non-HTML Mustache situations.
|
||||
* This may be useful in non-HTML Mustache situations.
|
||||
*/
|
||||
const PRAGMA_UNESCAPED = 'UNESCAPED';
|
||||
|
||||
|
@ -59,6 +59,8 @@ class Mustache {
|
|||
self::PRAGMA_UNESCAPED
|
||||
);
|
||||
|
||||
protected $_localPragmas;
|
||||
|
||||
/**
|
||||
* Mustache class constructor.
|
||||
*
|
||||
|
@ -77,6 +79,28 @@ class Mustache {
|
|||
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)) {
|
||||
$last = array_pop($keys);
|
||||
if ($this->_context[$last] instanceof Mustache) {
|
||||
$this->_context[$last] =& $this;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Render the given template and view object.
|
||||
*
|
||||
|
@ -99,6 +123,7 @@ class Mustache {
|
|||
$this->_context = array($this);
|
||||
}
|
||||
|
||||
$template = $this->_renderPragmas($template);
|
||||
return $this->_renderTemplate($template, $this->_context);
|
||||
}
|
||||
|
||||
|
@ -124,13 +149,11 @@ class Mustache {
|
|||
*
|
||||
* @access protected
|
||||
* @param string $template
|
||||
* @param array &$context
|
||||
* @return string Rendered Mustache template.
|
||||
*/
|
||||
protected function _renderTemplate($template, &$context) {
|
||||
$template = $this->_renderPragmas($template, $context);
|
||||
$template = $this->_renderSection($template, $context);
|
||||
return $this->_renderTags($template, $context);
|
||||
protected function _renderTemplate($template) {
|
||||
$template = $this->_renderSection($template);
|
||||
return $this->_renderTags($template);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -138,13 +161,12 @@ class Mustache {
|
|||
*
|
||||
* @access protected
|
||||
* @param string $template
|
||||
* @param array $context
|
||||
* @return string
|
||||
*/
|
||||
protected function _renderSection($template, &$context) {
|
||||
$otag = $this->_prepareRegEx($this->_otag);
|
||||
$ctag = $this->_prepareRegEx($this->_ctag);
|
||||
$regex = '/' . $otag . '(\\^|\\#)(.+?)' . $ctag . '\\s*([\\s\\S]+?)' . $otag . '\\/\\2' . $ctag . '\\s*/m';
|
||||
protected function _renderSection($template) {
|
||||
$otag = preg_quote($this->_otag, '/');
|
||||
$ctag = preg_quote($this->_ctag, '/');
|
||||
$regex = '/' . $otag . '(\\^|\\#)\\s*(.+?)\\s*' . $ctag . '\\s*([\\s\\S]+?)' . $otag . '\\/\\s*\\2\\s*' . $ctag . '\\s*/m';
|
||||
|
||||
$matches = array();
|
||||
while (preg_match($regex, $template, $matches, PREG_OFFSET_CAPTURE)) {
|
||||
|
@ -155,7 +177,7 @@ class Mustache {
|
|||
$content = $matches[3][0];
|
||||
|
||||
$replace = '';
|
||||
$val = $this->_getVariable($tag_name, $context);
|
||||
$val = $this->_getVariable($tag_name);
|
||||
switch($type) {
|
||||
// inverted section
|
||||
case '^':
|
||||
|
@ -168,13 +190,15 @@ class Mustache {
|
|||
case '#':
|
||||
if ($this->_varIsIterable($val)) {
|
||||
foreach ($val as $local_context) {
|
||||
$c = $this->_getContext($context, $local_context);
|
||||
$replace .= $this->_renderTemplate($content, $c);
|
||||
$this->_pushContext($local_context);
|
||||
$replace .= $this->_renderTemplate($content);
|
||||
$this->_popContext();
|
||||
}
|
||||
} else if ($val) {
|
||||
if (is_array($val) || is_object($val)) {
|
||||
$c = $this->_getContext($context, $val);
|
||||
$replace .= $this->_renderTemplate($content, $c);
|
||||
$this->_pushContext($val);
|
||||
$replace .= $this->_renderTemplate($content);
|
||||
$this->_popContext();
|
||||
} else {
|
||||
$replace .= $content;
|
||||
}
|
||||
|
@ -193,17 +217,18 @@ class Mustache {
|
|||
*
|
||||
* @access protected
|
||||
* @param string $template
|
||||
* @param array &$context
|
||||
* @return string
|
||||
*/
|
||||
protected function _renderPragmas($template, &$context) {
|
||||
protected function _renderPragmas($template) {
|
||||
$this->_localPragmas = $this->_pragmas;
|
||||
|
||||
// no pragmas
|
||||
if (strpos($template, $this->_otag . '%') === false) {
|
||||
return $template;
|
||||
}
|
||||
|
||||
$otag = $this->_prepareRegEx($this->_otag);
|
||||
$ctag = $this->_prepareRegEx($this->_ctag);
|
||||
$otag = preg_quote($this->_otag, '/');
|
||||
$ctag = preg_quote($this->_ctag, '/');
|
||||
$regex = '/' . $otag . '%\\s*([\\w_-]+)((?: [\\w]+=[\\w]+)*)\\s*' . $ctag . '\\n?/';
|
||||
return preg_replace_callback($regex, array($this, '_renderPragma'), $template);
|
||||
}
|
||||
|
@ -234,9 +259,9 @@ class Mustache {
|
|||
}
|
||||
|
||||
if (empty($options)) {
|
||||
$this->_pragmas[$pragma_name] = true;
|
||||
$this->_localPragmas[$pragma_name] = true;
|
||||
} else {
|
||||
$this->_pragmas[$pragma_name] = $options;
|
||||
$this->_localPragmas[$pragma_name] = $options;
|
||||
}
|
||||
|
||||
return '';
|
||||
|
@ -250,7 +275,7 @@ class Mustache {
|
|||
* @return bool
|
||||
*/
|
||||
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;
|
||||
} else {
|
||||
return false;
|
||||
|
@ -270,7 +295,7 @@ class Mustache {
|
|||
throw new MustacheException('Unknown pragma: ' . $pragma_name, MustacheException::UNKNOWN_PRAGMA);
|
||||
}
|
||||
|
||||
return $this->_pragmas[$pragma_name];
|
||||
return $this->_localPragmas[$pragma_name];
|
||||
}
|
||||
|
||||
|
||||
|
@ -292,16 +317,18 @@ class Mustache {
|
|||
*
|
||||
* @access protected
|
||||
* @param string $template
|
||||
* @param array $context
|
||||
* @return void
|
||||
*/
|
||||
protected function _renderTags($template, &$context) {
|
||||
protected function _renderTags($template) {
|
||||
if (strpos($template, $this->_otag) === false) {
|
||||
return $template;
|
||||
}
|
||||
|
||||
$otag = $this->_prepareRegEx($this->_otag);
|
||||
$ctag = $this->_prepareRegEx($this->_ctag);
|
||||
$otag_orig = $this->_otag;
|
||||
$ctag_orig = $this->_ctag;
|
||||
|
||||
$otag = preg_quote($this->_otag, '/');
|
||||
$ctag = preg_quote($this->_ctag, '/');
|
||||
|
||||
$this->_tagRegEx = '/' . $otag . "([#\^\/=!>\\{&])?(.+?)\\1?" . $ctag . "+/";
|
||||
|
||||
|
@ -314,10 +341,13 @@ class Mustache {
|
|||
$tag_name = trim($matches[2][0]);
|
||||
|
||||
$html .= substr($template, 0, $offset);
|
||||
$html .= $this->_renderTag($modifier, $tag_name, $context);
|
||||
$html .= $this->_renderTag($modifier, $tag_name);
|
||||
$template = substr($template, $offset + strlen($tag));
|
||||
}
|
||||
|
||||
$this->_otag = $otag_orig;
|
||||
$this->_ctag = $ctag_orig;
|
||||
|
||||
return $html . $template;
|
||||
}
|
||||
|
||||
|
@ -330,11 +360,10 @@ class Mustache {
|
|||
* @access protected
|
||||
* @param string $modifier
|
||||
* @param string $tag_name
|
||||
* @param array $context
|
||||
* @throws MustacheException Unmatched section tag encountered.
|
||||
* @return string
|
||||
*/
|
||||
protected function _renderTag($modifier, $tag_name, &$context) {
|
||||
protected function _renderTag($modifier, $tag_name) {
|
||||
switch ($modifier) {
|
||||
case '#':
|
||||
case '^':
|
||||
|
@ -352,28 +381,28 @@ class Mustache {
|
|||
}
|
||||
break;
|
||||
case '=':
|
||||
return $this->_changeDelimiter($tag_name, $context);
|
||||
return $this->_changeDelimiter($tag_name);
|
||||
break;
|
||||
case '!':
|
||||
return $this->_renderComment($tag_name, $context);
|
||||
return $this->_renderComment($tag_name);
|
||||
break;
|
||||
case '>':
|
||||
return $this->_renderPartial($tag_name, $context);
|
||||
return $this->_renderPartial($tag_name);
|
||||
break;
|
||||
case '{':
|
||||
case '&':
|
||||
if ($this->_hasPragma(self::PRAGMA_UNESCAPED)) {
|
||||
return $this->_renderEscaped($tag_name, $context);
|
||||
return $this->_renderEscaped($tag_name);
|
||||
} else {
|
||||
return $this->_renderUnescaped($tag_name, $context);
|
||||
return $this->_renderUnescaped($tag_name);
|
||||
}
|
||||
break;
|
||||
case '':
|
||||
default:
|
||||
if ($this->_hasPragma(self::PRAGMA_UNESCAPED)) {
|
||||
return $this->_renderUnescaped($tag_name, $context);
|
||||
return $this->_renderUnescaped($tag_name);
|
||||
} else {
|
||||
return $this->_renderEscaped($tag_name, $context);
|
||||
return $this->_renderEscaped($tag_name);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
@ -384,11 +413,10 @@ class Mustache {
|
|||
*
|
||||
* @access protected
|
||||
* @param string $tag_name
|
||||
* @param array $context
|
||||
* @return string
|
||||
*/
|
||||
protected function _renderEscaped($tag_name, &$context) {
|
||||
return htmlentities($this->_getVariable($tag_name, $context), null, $this->_charset);
|
||||
protected function _renderEscaped($tag_name) {
|
||||
return htmlentities($this->_getVariable($tag_name), null, $this->_charset);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -396,10 +424,9 @@ class Mustache {
|
|||
*
|
||||
* @access protected
|
||||
* @param string $tag_name
|
||||
* @param array $context
|
||||
* @return string
|
||||
*/
|
||||
protected function _renderComment($tag_name, &$context) {
|
||||
protected function _renderComment($tag_name) {
|
||||
return '';
|
||||
}
|
||||
|
||||
|
@ -408,11 +435,10 @@ class Mustache {
|
|||
*
|
||||
* @access protected
|
||||
* @param string $tag_name
|
||||
* @param array $context
|
||||
* @return string
|
||||
*/
|
||||
protected function _renderUnescaped($tag_name, &$context) {
|
||||
return $this->_getVariable($tag_name, $context);
|
||||
protected function _renderUnescaped($tag_name) {
|
||||
return $this->_getVariable($tag_name);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -420,14 +446,11 @@ class Mustache {
|
|||
*
|
||||
* @access protected
|
||||
* @param string $tag_name
|
||||
* @param array $context
|
||||
* @return string
|
||||
*/
|
||||
protected function _renderPartial($tag_name, &$context) {
|
||||
$view = new self($this->_getPartial($tag_name), $this->_flattenContext($context), $this->_partials);
|
||||
$view->_otag = $this->_otag;
|
||||
$view->_ctag = $this->_ctag;
|
||||
return $view->render();
|
||||
protected function _renderPartial($tag_name) {
|
||||
$view = clone($this);
|
||||
return $view->render($this->_getPartial($tag_name));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -436,77 +459,51 @@ class Mustache {
|
|||
*
|
||||
* @access protected
|
||||
* @param string $tag_name
|
||||
* @param array $context
|
||||
* @return string
|
||||
*/
|
||||
protected function _changeDelimiter($tag_name, &$context) {
|
||||
protected function _changeDelimiter($tag_name) {
|
||||
$tags = explode(' ', $tag_name);
|
||||
$this->_otag = $tags[0];
|
||||
$this->_ctag = $tags[1];
|
||||
|
||||
$otag = $this->_prepareRegEx($this->_otag);
|
||||
$ctag = $this->_prepareRegEx($this->_ctag);
|
||||
$otag = preg_quote($this->_otag, '/');
|
||||
$ctag = preg_quote($this->_ctag, '/');
|
||||
$this->_tagRegEx = '/' . $otag . "([#\^\/=!>\\{&])?(.+?)\\1?" . $ctag . "+/";
|
||||
return '';
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Prepare a new context reference array.
|
||||
*
|
||||
* This is used to create context arrays for iterable blocks.
|
||||
* Push a local context onto the stack.
|
||||
*
|
||||
* @access protected
|
||||
* @param array $context
|
||||
* @param array $local_context
|
||||
* @return array
|
||||
* @param array &$local_context
|
||||
* @return void
|
||||
*/
|
||||
protected function _getContext(&$context, &$local_context) {
|
||||
$ret = array();
|
||||
$ret[] =& $local_context;
|
||||
foreach ($context as $view) {
|
||||
$ret[] =& $view;
|
||||
protected function _pushContext(&$local_context) {
|
||||
$new = array();
|
||||
$new[] =& $local_context;
|
||||
foreach (array_keys($this->_context) as $key) {
|
||||
$new[] =& $this->_context[$key];
|
||||
}
|
||||
return $ret;
|
||||
$this->_context = $new;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Prepare a new (flattened) context.
|
||||
*
|
||||
* This is used to create a view object or array for rendering partials.
|
||||
* Remove the latest context from the stack.
|
||||
*
|
||||
* @access protected
|
||||
* @param array &$context
|
||||
* @return array
|
||||
* @throws MustacheException
|
||||
* @return void
|
||||
*/
|
||||
protected function _flattenContext(&$context) {
|
||||
$keys = array_keys($context);
|
||||
$first = $context[$keys[0]];
|
||||
protected function _popContext() {
|
||||
$new = array();
|
||||
|
||||
if ($first instanceof Mustache) {
|
||||
$ret = clone $first;
|
||||
unset($keys[0]);
|
||||
|
||||
foreach ($keys as $name) {
|
||||
foreach ($context[$name] as $key => $val) {
|
||||
$ret->$key =& $val;
|
||||
}
|
||||
}
|
||||
} else if (is_array($first)) {
|
||||
$ret = array();
|
||||
|
||||
foreach ($keys as $name) {
|
||||
foreach ($context[$name] as $key => $val) {
|
||||
$ret[$key] =& $val;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
throw new MustacheException('Unknown root context type.');
|
||||
$keys = array_keys($this->_context);
|
||||
array_shift($keys);
|
||||
foreach ($keys as $key) {
|
||||
$new[] =& $this->_context[$key];
|
||||
}
|
||||
|
||||
return $ret;
|
||||
$this->_context = $new;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -520,16 +517,15 @@ class Mustache {
|
|||
*
|
||||
* @access protected
|
||||
* @param string $tag_name
|
||||
* @param array $context
|
||||
* @throws MustacheException Unknown variable name.
|
||||
* @return string
|
||||
*/
|
||||
protected function _getVariable($tag_name, &$context) {
|
||||
protected function _getVariable($tag_name) {
|
||||
if ($this->_hasPragma(self::PRAGMA_DOT_NOTATION)) {
|
||||
$chunks = explode('.', $tag_name);
|
||||
$first = array_shift($chunks);
|
||||
|
||||
$ret = $this->_findVariableInContext($first, $context);
|
||||
$ret = $this->_findVariableInContext($first, $this->_context);
|
||||
while ($next = array_shift($chunks)) {
|
||||
// Slice off a chunk of context for dot notation traversal.
|
||||
$c = array($ret);
|
||||
|
@ -537,7 +533,7 @@ class Mustache {
|
|||
}
|
||||
return $ret;
|
||||
} else {
|
||||
return $this->_findVariableInContext($tag_name, $context);
|
||||
return $this->_findVariableInContext($tag_name, $this->_context);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -547,11 +543,11 @@ class Mustache {
|
|||
*
|
||||
* @access protected
|
||||
* @param string $tag_name
|
||||
* @param array &$context
|
||||
* @param array $context
|
||||
* @throws MustacheException Unknown variable name.
|
||||
* @return string
|
||||
*/
|
||||
protected function _findVariableInContext($tag_name, &$context) {
|
||||
protected function _findVariableInContext($tag_name, $context) {
|
||||
foreach ($context as $view) {
|
||||
if (is_object($view)) {
|
||||
if (isset($view->$tag_name)) {
|
||||
|
@ -603,22 +599,6 @@ class Mustache {
|
|||
protected function _varIsIterable($var) {
|
||||
return is_object($var) || (is_array($var) && !array_diff_key($var, array_keys(array_keys($var))));
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare a string to be used in a regular expression.
|
||||
*
|
||||
* @access protected
|
||||
* @param string $str
|
||||
* @return string
|
||||
*/
|
||||
protected function _prepareRegEx($str) {
|
||||
$replace = array(
|
||||
'\\' => '\\\\', '^' => '\^', '.' => '\.', '$' => '\$', '|' => '\|', '(' => '\(',
|
||||
')' => '\)', '[' => '\[', ']' => '\]', '*' => '\*', '+' => '\+', '?' => '\?',
|
||||
'{' => '\{', '}' => '\}', ',' => '\,'
|
||||
);
|
||||
return strtr($str, $replace);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue