Merge branch 'dev' into feature/implicit-iterator

Conflicts:
	Mustache.php
This commit is contained in:
Justin Hileman
2010-05-23 00:15:16 -04:00
18 changed files with 469 additions and 118 deletions
+206 -108
View File
@@ -14,30 +14,51 @@
*/ */
class Mustache { class Mustache {
public $otag = '{{'; public $_otag = '{{';
public $ctag = '}}'; public $_ctag = '}}';
// Should this Mustache throw exceptions when it finds unexpected tags? /**
protected $throwSectionExceptions = true; * Should this Mustache throw exceptions when it finds unexpected tags?
protected $throwPartialExceptions = false; *
protected $throwVariableExceptions = false; * @see self::_throwsException()
*/
protected $_throwsExceptions = array(
MustacheException::UNKNOWN_VARIABLE => false,
MustacheException::UNCLOSED_SECTION => true,
MustacheException::UNEXPECTED_CLOSE_SECTION => true,
MustacheException::UNKNOWN_PARTIAL => false,
MustacheException::UNKNOWN_PRAGMA => true,
);
// Override charset passed to htmlentities() and htmlspecialchars(). Defaults to UTF-8. // Override charset passed to htmlentities() and htmlspecialchars(). Defaults to UTF-8.
protected $charset = 'UTF-8'; protected $_charset = 'UTF-8';
const PRAGMA_DOT_NOTATION = 'DOT-NOTATION'; const PRAGMA_DOT_NOTATION = 'DOT-NOTATION';
const PRAGMA_IMPLICIT_ITERATOR = 'IMPLICIT-ITERATOR'; const PRAGMA_IMPLICIT_ITERATOR = 'IMPLICIT-ITERATOR';
protected $tagRegEx; /**
* The {{%UNESCAPED}} pragma swaps the meaning of the {{normal}} and {{{unescaped}}}
* Mustache tags. That is, once this pragma is activated the {{normal}} tag will not be
* escaped while the {{{unescaped}}} tag will be escaped.
*
* Pragmas apply only to the current template. Partials, even those included after the
* {{%UNESCAPED}} call, will need their own pragma declaration.
*
* This may be useful in non-HTML Mustache situations.
*/
const PRAGMA_UNESCAPED = 'UNESCAPED';
protected $template = ''; protected $_tagRegEx;
protected $context = array();
protected $partials = array();
protected $pragmas = array();
protected $pragmasImplemented = array( protected $_template = '';
protected $_context = array();
protected $_partials = array();
protected $_pragmas = array();
protected $_pragmasImplemented = array(
self::PRAGMA_DOT_NOTATION, self::PRAGMA_DOT_NOTATION,
self::PRAGMA_IMPLICIT_ITERATOR self::PRAGMA_IMPLICIT_ITERATOR,
self::PRAGMA_UNESCAPED
); );
/** /**
@@ -53,9 +74,9 @@ class Mustache {
* @return void * @return void
*/ */
public function __construct($template = null, $view = null, $partials = null) { public function __construct($template = null, $view = null, $partials = null) {
if ($template !== null) $this->template = $template; if ($template !== null) $this->_template = $template;
if ($partials !== null) $this->partials = $partials; if ($partials !== null) $this->_partials = $partials;
if ($view !== null) $this->context = array($view); if ($view !== null) $this->_context = array($view);
} }
/** /**
@@ -71,16 +92,17 @@ class Mustache {
* @return string Rendered Mustache template. * @return string Rendered Mustache template.
*/ */
public function render($template = null, $view = null, $partials = null) { public function render($template = null, $view = null, $partials = null) {
if ($template === null) $template = $this->template; if ($template === null) $template = $this->_template;
if ($partials !== null) $this->partials = $partials; if ($partials !== null) $this->_partials = $partials;
if ($view) { if ($view) {
$this->context = array($view); $this->_context = array($view);
} else if (empty($this->context)) { } else if (empty($this->_context)) {
$this->context = array($this); $this->_context = array($this);
} }
return $this->_render($template, $this->context); $template = $this->_renderPragmas($template, $context);
return $this->_renderTemplate($template, $this->_context);
} }
/** /**
@@ -100,7 +122,6 @@ class Mustache {
} }
} }
/** /**
* Internal render function, used for recursive calls. * Internal render function, used for recursive calls.
* *
@@ -109,10 +130,9 @@ class Mustache {
* @param array &$context * @param array &$context
* @return string Rendered Mustache template. * @return string Rendered Mustache template.
*/ */
protected function _render($template, &$context) { protected function _renderTemplate($template, &$context) {
$template = $this->renderPragmas($template, $context); $template = $this->_renderSection($template, $context);
$template = $this->renderSection($template, $context); return $this->_renderTags($template, $context);
return $this->renderTags($template, $context);
} }
/** /**
@@ -123,14 +143,10 @@ class Mustache {
* @param array $context * @param array $context
* @return string * @return string
*/ */
protected function renderSection($template, &$context) { protected function _renderSection($template, &$context) {
if (strpos($template, $this->otag . '#') === false) { $otag = $this->_prepareRegEx($this->_otag);
return $template; $ctag = $this->_prepareRegEx($this->_ctag);
} $regex = '/' . $otag . '(\\^|\\#)\\s*(.+?)\\s*' . $ctag . '\\s*([\\s\\S]+?)' . $otag . '\\/\\s*\\2\\s*' . $ctag . '\\s*/m';
$otag = $this->prepareRegEx($this->otag);
$ctag = $this->prepareRegEx($this->ctag);
$regex = '/' . $otag . '(\\^|\\#)(.+?)' . $ctag . '\\s*([\\s\\S]+?)' . $otag . '\\/\\2' . $ctag . '\\s*/m';
$matches = array(); $matches = array();
while (preg_match($regex, $template, $matches, PREG_OFFSET_CAPTURE)) { while (preg_match($regex, $template, $matches, PREG_OFFSET_CAPTURE)) {
@@ -141,7 +157,7 @@ class Mustache {
$content = $matches[3][0]; $content = $matches[3][0];
$replace = ''; $replace = '';
$val = $this->getVariable($tag_name, $context); $val = $this->_getVariable($tag_name, $context);
switch($type) { switch($type) {
// inverted section // inverted section
case '^': case '^':
@@ -152,9 +168,9 @@ class Mustache {
// regular section // regular section
case '#': case '#':
if ($this->varIsIterable($val)) { if ($this->_varIsIterable($val)) {
if ($this->hasPragma(self::PRAGMA_IMPLICIT_ITERATOR)) { if ($this->_hasPragma(self::PRAGMA_IMPLICIT_ITERATOR)) {
if ($opt = $this->getPragmaOptions(self::PRAGMA_IMPLICIT_ITERATOR)) { if ($opt = $this->_getPragmaOptions(self::PRAGMA_IMPLICIT_ITERATOR)) {
$iterator = $opt['iterator']; $iterator = $opt['iterator'];
} else { } else {
$iterator = '.'; $iterator = '.';
@@ -165,15 +181,18 @@ class Mustache {
foreach ($val as $local_context) { foreach ($val as $local_context) {
if ($iterator) { if ($iterator) {
$c = array($iterator => $local_context); $c1 = array($iterator => $local_context);
$replace .= $this->_render($content, $this->getContext($context, $c)); $c2 = $this->_getContext($context, $c1);
$replace .= $this->_renderTemplate($content, $c2);
} else { } else {
$replace .= $this->_render($content, $this->getContext($context, $local_context)); $c = $this->_getContext($context, $local_context);
$replace .= $this->_renderTemplate($content, $c);
} }
} }
} else if ($val) { } else if ($val) {
if (is_array($val) || is_object($val)) { if (is_array($val) || is_object($val)) {
$replace .= $this->_render($content, $this->getContext($context, $val)); $c = $this->_getContext($context, $val);
$replace .= $this->_renderTemplate($content, $c);
} else { } else {
$replace .= $content; $replace .= $content;
} }
@@ -195,16 +214,16 @@ class Mustache {
* @param array &$context * @param array &$context
* @return string * @return string
*/ */
protected function renderPragmas($template, &$context) { protected function _renderPragmas($template, &$context) {
// no pragmas // no pragmas
if (strpos($template, $this->otag . '%') === false) { if (strpos($template, $this->_otag . '%') === false) {
return $template; return $template;
} }
$otag = $this->prepareRegEx($this->otag); $otag = $this->_prepareRegEx($this->_otag);
$ctag = $this->prepareRegEx($this->ctag); $ctag = $this->_prepareRegEx($this->_ctag);
$regex = '/' . $otag . '%([\\w_-]+)((?: [\\w]+=[\\w]+)*)' . $ctag . '\\n?/'; $regex = '/' . $otag . '%\\s*([\\w_-]+)((?: [\\w]+=[\\w]+)*)\\s*' . $ctag . '\\n?/';
return preg_replace_callback($regex, array($this, 'renderPragma'), $template); return preg_replace_callback($regex, array($this, '_renderPragma'), $template);
} }
/** /**
@@ -215,12 +234,12 @@ class Mustache {
* @return void * @return void
* @throws MustacheException unknown pragma * @throws MustacheException unknown pragma
*/ */
protected function renderPragma($matches) { protected function _renderPragma($matches) {
$pragma = $matches[0]; $pragma = $matches[0];
$pragma_name = $matches[1]; $pragma_name = $matches[1];
$options_string = $matches[2]; $options_string = $matches[2];
if (!in_array($pragma_name, $this->pragmasImplemented)) { if (!in_array($pragma_name, $this->_pragmasImplemented)) {
throw new MustacheException('Unknown pragma: ' . $pragma_name, MustacheException::UNKNOWN_PRAGMA); throw new MustacheException('Unknown pragma: ' . $pragma_name, MustacheException::UNKNOWN_PRAGMA);
} }
@@ -233,26 +252,57 @@ class Mustache {
} }
if (empty($options)) { if (empty($options)) {
$this->pragmas[$pragma_name] = true; $this->_pragmas[$pragma_name] = true;
} else { } else {
$this->pragmas[$pragma_name] = $options; $this->_pragmas[$pragma_name] = $options;
} }
return ''; return '';
} }
protected function hasPragma($pragma_name) { /**
if (array_key_exists($pragma_name, $this->pragmas) && $this->pragmas[$pragma_name]) { * Check whether this Mustache has a specific pragma.
*
* @access protected
* @param string $pragma_name
* @return bool
*/
protected function _hasPragma($pragma_name) {
if (array_key_exists($pragma_name, $this->_pragmas) && $this->_pragmas[$pragma_name]) {
return true; return true;
} else {
return false;
} }
} }
protected function getPragmaOptions($pragma_name) { /**
if (!$this->hasPragma($pragma_name)) { * Return pragma options, if any.
*
* @access protected
* @param string $pragma_name
* @return mixed
* @throws MustacheException Unknown pragma
*/
protected function _getPragmaOptions($pragma_name) {
if (!$this->_hasPragma($pragma_name)) {
throw new MustacheException('Unknown pragma: ' . $pragma_name, MustacheException::UNKNOWN_PRAGMA); throw new MustacheException('Unknown pragma: ' . $pragma_name, MustacheException::UNKNOWN_PRAGMA);
} }
return (is_array($this->pragmas[$pragma_name])) ? $this->pragmas[$pragma_name] : array(); return (is_array($this->_pragmas[$pragma_name])) ? $this->_pragmas[$pragma_name] : array();
}
/**
* Check whether this Mustache instance throws a given exception.
*
* Expects exceptions to be MustacheException error codes (i.e. class constants).
*
* @access protected
* @param mixed $exception
* @return void
*/
protected function _throwsException($exception) {
return (isset($this->_throwsExceptions[$exception]) && $this->_throwsExceptions[$exception]);
} }
/** /**
@@ -263,24 +313,26 @@ class Mustache {
* @param array $context * @param array $context
* @return void * @return void
*/ */
protected function renderTags($template, &$context) { protected function _renderTags($template, &$context) {
if (strpos($template, $this->otag) === false) { if (strpos($template, $this->_otag) === false) {
return $template; return $template;
} }
$otag = $this->prepareRegEx($this->otag); $otag = $this->_prepareRegEx($this->_otag);
$ctag = $this->prepareRegEx($this->ctag); $ctag = $this->_prepareRegEx($this->_ctag);
$this->tagRegEx = '/' . $otag . "(#|\/|=|!|>|\\{|&)?([^\/#]+?)\\1?" . $ctag . "+/";
$this->_tagRegEx = '/' . $otag . "([#\^\/=!>\\{&])?(.+?)\\1?" . $ctag . "+/";
$html = ''; $html = '';
$matches = array(); $matches = array();
while (preg_match($this->tagRegEx, $template, $matches, PREG_OFFSET_CAPTURE)) { while (preg_match($this->_tagRegEx, $template, $matches, PREG_OFFSET_CAPTURE)) {
$tag = $matches[0][0]; $tag = $matches[0][0];
$offset = $matches[0][1]; $offset = $matches[0][1];
$modifier = $matches[1][0]; $modifier = $matches[1][0];
$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, $context);
$template = substr($template, $offset + strlen($tag)); $template = substr($template, $offset + strlen($tag));
} }
@@ -300,38 +352,47 @@ class Mustache {
* @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, &$context) {
switch ($modifier) { switch ($modifier) {
case '#': case '#':
if ($this->throwSectionExceptions) { case '^':
if ($this->_throwsException(MustacheException::UNCLOSED_SECTION)) {
throw new MustacheException('Unclosed section: ' . $tag_name, MustacheException::UNCLOSED_SECTION); throw new MustacheException('Unclosed section: ' . $tag_name, MustacheException::UNCLOSED_SECTION);
} else { } else {
return ''; return '';
} }
break; break;
case '/': case '/':
if ($this->throwSectionExceptions) { if ($this->_throwsException(MustacheException::UNEXPECTED_CLOSE_SECTION)) {
throw new MustacheException('Unexpected close section: ' . $tag_name, MustacheException::UNEXPECTED_CLOSE_SECTION); throw new MustacheException('Unexpected close section: ' . $tag_name, MustacheException::UNEXPECTED_CLOSE_SECTION);
} else { } else {
return ''; return '';
} }
break; break;
case '=': case '=':
return $this->changeDelimiter($tag_name, $context); return $this->_changeDelimiter($tag_name, $context);
break; break;
case '!': case '!':
return $this->renderComment($tag_name, $context); return $this->_renderComment($tag_name, $context);
break; break;
case '>': case '>':
return $this->renderPartial($tag_name, $context); return $this->_renderPartial($tag_name, $context);
break; break;
case '{': case '{':
case '&': case '&':
return $this->renderUnescaped($tag_name, $context); if ($this->_hasPragma(self::PRAGMA_UNESCAPED)) {
return $this->_renderEscaped($tag_name, $context);
} else {
return $this->_renderUnescaped($tag_name, $context);
}
break; break;
case '': case '':
default: default:
return $this->renderEscaped($tag_name, $context); if ($this->_hasPragma(self::PRAGMA_UNESCAPED)) {
return $this->_renderUnescaped($tag_name, $context);
} else {
return $this->_renderEscaped($tag_name, $context);
}
break; break;
} }
} }
@@ -344,8 +405,8 @@ class Mustache {
* @param array $context * @param array $context
* @return string * @return string
*/ */
protected function renderEscaped($tag_name, &$context) { protected function _renderEscaped($tag_name, &$context) {
return htmlentities($this->getVariable($tag_name, $context), null, $this->charset); return htmlentities($this->_getVariable($tag_name, $context), null, $this->_charset);
} }
/** /**
@@ -356,7 +417,7 @@ class Mustache {
* @param array $context * @param array $context
* @return string * @return string
*/ */
protected function renderComment($tag_name, &$context) { protected function _renderComment($tag_name, &$context) {
return ''; return '';
} }
@@ -368,8 +429,8 @@ class Mustache {
* @param array $context * @param array $context
* @return string * @return string
*/ */
protected function renderUnescaped($tag_name, &$context) { protected function _renderUnescaped($tag_name, &$context) {
return $this->getVariable($tag_name, $context); return $this->_getVariable($tag_name, $context);
} }
/** /**
@@ -380,10 +441,10 @@ class Mustache {
* @param array $context * @param array $context
* @return string * @return string
*/ */
protected function renderPartial($tag_name, &$context) { protected function _renderPartial($tag_name, &$context) {
$view = new self($this->getPartial($tag_name), $context, $this->partials); $view = new self($this->_getPartial($tag_name), $this->_flattenContext($context), $this->_partials);
$view->otag = $this->otag; $view->_otag = $this->_otag;
$view->ctag = $this->ctag; $view->_ctag = $this->_ctag;
return $view->render(); return $view->render();
} }
@@ -396,14 +457,14 @@ class Mustache {
* @param array $context * @param array $context
* @return string * @return string
*/ */
protected function changeDelimiter($tag_name, &$context) { protected function _changeDelimiter($tag_name, &$context) {
$tags = explode(' ', $tag_name); $tags = explode(' ', $tag_name);
$this->otag = $tags[0]; $this->_otag = $tags[0];
$this->ctag = $tags[1]; $this->_ctag = $tags[1];
$otag = $this->prepareRegEx($this->otag); $otag = $this->_prepareRegEx($this->_otag);
$ctag = $this->prepareRegEx($this->ctag); $ctag = $this->_prepareRegEx($this->_ctag);
$this->tagRegEx = '/' . $otag . "(#|\/|=|!|>|\\{|&)?([^\/#\^]+?)\\1?" . $ctag . "+/"; $this->_tagRegEx = '/' . $otag . "([#\^\/=!>\\{&])?(.+?)\\1?" . $ctag . "+/";
return ''; return '';
} }
@@ -415,10 +476,10 @@ class Mustache {
* *
* @access protected * @access protected
* @param array $context * @param array $context
* @param mixed $local_context * @param array $local_context
* @return void * @return array
*/ */
protected function getContext(&$context, &$local_context) { protected function _getContext(&$context, &$local_context) {
$ret = array(); $ret = array();
$ret[] =& $local_context; $ret[] =& $local_context;
foreach ($context as $view) { foreach ($context as $view) {
@@ -427,6 +488,45 @@ class Mustache {
return $ret; return $ret;
} }
/**
* Prepare a new (flattened) context.
*
* This is used to create a view object or array for rendering partials.
*
* @access protected
* @param array &$context
* @return array
* @throws MustacheException
*/
protected function _flattenContext(&$context) {
$keys = array_keys($context);
$first = $context[$keys[0]];
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.');
}
return $ret;
}
/** /**
* Get a variable from the context array. * Get a variable from the context array.
* *
@@ -442,22 +542,20 @@ class Mustache {
* @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, &$context) {
if ($ret = $this->_getVariable($tag_name, $context)) { if ($this->_hasPragma(self::PRAGMA_DOT_NOTATION) && $tag_name != '.') {
return $ret;
} else 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->_getVariable($first, $context); $ret = $this->_findVariableInContext($first, $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);
$ret = $this->_getVariable($next, $c); $ret = $this->_findVariableInContext($next, $c);
} }
return $ret; return $ret;
} else { } else {
return $ret; return $this->_findVariableInContext($tag_name, $context);
} }
} }
@@ -471,7 +569,7 @@ class Mustache {
* @throws MustacheException Unknown variable name. * @throws MustacheException Unknown variable name.
* @return string * @return string
*/ */
protected function _getVariable($tag_name, &$context) { protected function _findVariableInContext($tag_name, &$context) {
foreach ($context as $view) { foreach ($context as $view) {
if (is_object($view)) { if (is_object($view)) {
if (isset($view->$tag_name)) { if (isset($view->$tag_name)) {
@@ -484,7 +582,7 @@ class Mustache {
} }
} }
if ($this->throwVariableExceptions) { if ($this->_throwsException(MustacheException::UNKNOWN_VARIABLE)) {
throw new MustacheException("Unknown variable: " . $tag_name, MustacheException::UNKNOWN_VARIABLE); throw new MustacheException("Unknown variable: " . $tag_name, MustacheException::UNKNOWN_VARIABLE);
} else { } else {
return ''; return '';
@@ -501,12 +599,12 @@ class Mustache {
* @throws MustacheException Unknown partial name. * @throws MustacheException Unknown partial name.
* @return string * @return string
*/ */
protected function getPartial($tag_name) { protected function _getPartial($tag_name) {
if (is_array($this->partials) && isset($this->partials[$tag_name])) { if (is_array($this->_partials) && isset($this->_partials[$tag_name])) {
return $this->partials[$tag_name]; return $this->_partials[$tag_name];
} }
if ($this->throwPartialExceptions) { if ($this->_throwsException(MustacheException::UNKNOWN_PARTIAL)) {
throw new MustacheException('Unknown partial: ' . $tag_name, MustacheException::UNKNOWN_PARTIAL); throw new MustacheException('Unknown partial: ' . $tag_name, MustacheException::UNKNOWN_PARTIAL);
} else { } else {
return ''; return '';
@@ -520,7 +618,7 @@ class Mustache {
* @param mixed $var * @param mixed $var
* @return bool * @return bool
*/ */
protected function varIsIterable($var) { protected function _varIsIterable($var) {
return is_object($var) || (is_array($var) && !array_diff_key($var, array_keys(array_keys($var)))); return is_object($var) || (is_array($var) && !array_diff_key($var, array_keys(array_keys($var))));
} }
@@ -531,7 +629,7 @@ class Mustache {
* @param string $str * @param string $str
* @return string * @return string
*/ */
protected function prepareRegEx($str) { protected function _prepareRegEx($str) {
$replace = array( $replace = array(
'\\' => '\\\\', '^' => '\^', '.' => '\.', '$' => '\$', '|' => '\|', '(' => '\(', '\\' => '\\\\', '^' => '\^', '.' => '\.', '$' => '\$', '|' => '\|', '(' => '\(',
')' => '\)', '[' => '\[', ']' => '\]', '*' => '\*', '+' => '\+', '?' => '\?', ')' => '\)', '[' => '\[', ']' => '\]', '*' => '\*', '+' => '\+', '?' => '\?',
@@ -557,7 +655,7 @@ class MustacheException extends Exception {
const UNCLOSED_SECTION = 1; const UNCLOSED_SECTION = 1;
// An UNEXPECTED_CLOSE_SECTION exception is thrown when {{/section}} appears // An UNEXPECTED_CLOSE_SECTION exception is thrown when {{/section}} appears
// without a corresponding {{#section}}. // without a corresponding {{#section}} or {{^section}}.
const UNEXPECTED_CLOSE_SECTION = 2; const UNEXPECTED_CLOSE_SECTION = 2;
// An UNKNOWN_PARTIAL exception is thrown whenever a {{>partial}} tag appears // An UNKNOWN_PARTIAL exception is thrown whenever a {{>partial}} tag appears
+2 -2
View File
@@ -81,8 +81,8 @@ And render it:
Known Issues Known Issues
------------ ------------
* There's no way to toggle a pragma other than checking out the feature branch for each pragma... * Pragmas don't un-apply... Instead of applying only to a specific template, pragmas are applied
Need a clean way to do this. to all subsequent templates and partials rendered by this Mustache instance.
* Sections don't respect delimiter changes -- `delimiters` example currently fails with an * Sections don't respect delimiter changes -- `delimiters` example currently fails with an
"unclosed section" exception. "unclosed section" exception.
* Test coverage is incomplete. * Test coverage is incomplete.
+1 -1
View File
@@ -1 +1 @@
<h1>{{title}}{{! just something interesting... or not... }}</h1> <h1>{{title}}{{! just something interesting... #or ^not... }}</h1>
@@ -0,0 +1,6 @@
<?php
class InvertedDoubleSection extends Mustache {
public $t = false;
public $two = 'second';
}
@@ -0,0 +1,7 @@
{{^t}}
* first
{{/t}}
* {{two}}
{{^t}}
* third
{{/t}}
@@ -0,0 +1,3 @@
* first
* second
* third
+13
View File
@@ -0,0 +1,13 @@
<?php
class Partials extends Mustache {
public $name = 'ilmich';
public $data = array(
array('name' => 'federica', 'age' => 27, 'gender' => 'female'),
array('name' => 'marco', 'age' => 32, 'gender' => 'male'),
);
protected $_partials = array(
'children' => "{{#data}}{{name}} - {{age}} - {{gender}}\n{{/data}}",
);
}
+2
View File
@@ -0,0 +1,2 @@
Children of {{name}}:
{{>children}}
+3
View File
@@ -0,0 +1,3 @@
Children of ilmich:
federica - 27 - female
marco - 32 - male
@@ -0,0 +1,5 @@
<?php
class PragmaUnescaped extends Mustache {
protected $vs = 'Bear > Shark';
}
@@ -0,0 +1,3 @@
{{%UNESCAPED}}
{{vs}}
{{{vs}}}
@@ -0,0 +1,2 @@
Bear > Shark
Bear &gt; Shark
+37
View File
@@ -0,0 +1,37 @@
<?php
/**
* Whitespace test for tag names.
*
* Per http://github.com/janl/mustache.js/issues/issue/34/#comment_244396
* tags should strip leading and trailing whitespace in key names.
*
* `{{> tag }}` and `{{> tag}}` and `{{>tag}}` should all be equivalent.
*
* @extends Mustache
*/
class Whitespace extends Mustache {
public $foo = 'alpha';
public $bar = 'beta';
public function baz() {
return 'gamma';
}
public function qux() {
return array(
array('key with space' => 'A'),
array('key with space' => 'B'),
array('key with space' => 'C'),
array('key with space' => 'D'),
array('key with space' => 'E'),
array('key with space' => 'F'),
array('key with space' => 'G'),
);
}
protected $_partials = array(
'alphabet' => " * {{.}}\n",
);
}
+10
View File
@@ -0,0 +1,10 @@
{{^ inverted section test }}
These are some things:
{{/inverted section test }}
* {{ foo }}
* {{ bar}}
* {{ baz }}
{{# qux }}
* {{ key with space }}
{{/ qux }}
{{#qux}}.{{/qux}}
+12
View File
@@ -0,0 +1,12 @@
These are some things:
* alpha
* beta
* gamma
* A
* B
* C
* D
* E
* F
* G
.......
+9 -6
View File
@@ -20,20 +20,23 @@ class MustachePragmaTest extends PHPUnit_Framework_TestCase {
public function testPragmaReplace() { public function testPragmaReplace() {
$m = new Mustache(); $m = new Mustache();
$this->assertEquals($m->render('{{%DOT-NOTATION}}'), '', 'Pragma tag not removed'); $this->assertEquals('', $m->render('{{%DOT-NOTATION}}'), 'Pragma tag not removed');
} }
public function testPragmaReplaceMultiple() { public function testPragmaReplaceMultiple() {
$m = new Mustache(); $m = new Mustache();
$this->assertEquals($m->render("{{%DOT-NOTATION}}\n{{%DOT-NOTATION}}"), '', 'Multiple pragma tags not removed');
$this->assertEquals($m->render('{{%DOT-NOTATION}} {{%DOT-NOTATION}}'), ' ', 'Multiple pragma tags not removed'); $this->assertEquals('', $m->render('{{% DOT-NOTATION }}'), 'Pragmas should allow whitespace');
$this->assertEquals('', $m->render('{{% DOT-NOTATION foo=bar }}'), 'Pragmas should allow whitespace');
$this->assertEquals('', $m->render("{{%DOT-NOTATION}}\n{{%DOT-NOTATION}}"), 'Multiple pragma tags not removed');
$this->assertEquals(' ', $m->render('{{%DOT-NOTATION}} {{%DOT-NOTATION}}'), 'Multiple pragma tags not removed');
} }
public function testPragmaReplaceNewline() { public function testPragmaReplaceNewline() {
$m = new Mustache(); $m = new Mustache();
$this->assertEquals($m->render("{{%DOT-NOTATION}}\n"), '', 'Trailing newline after pragma tag not removed'); $this->assertEquals('', $m->render("{{%DOT-NOTATION}}\n"), 'Trailing newline after pragma tag not removed');
$this->assertEquals($m->render("\n{{%DOT-NOTATION}}\n"), "\n", 'Too many newlines removed with pragma tag'); $this->assertEquals("\n", $m->render("\n{{%DOT-NOTATION}}\n"), 'Too many newlines removed with pragma tag');
$this->assertEquals($m->render("1\n2{{%DOT-NOTATION}}\n3"), "1\n23", 'Wrong newline removed with pragma tag'); $this->assertEquals("1\n23", $m->render("1\n2{{%DOT-NOTATION}}\n3"), 'Wrong newline removed with pragma tag');
} }
} }
+15
View File
@@ -0,0 +1,15 @@
<?php
require_once '../Mustache.php';
require_once 'PHPUnit/Framework.php';
class MustachePragmaUnescapedTest extends PHPUnit_Framework_TestCase {
public function testPragmaUnescaped() {
$m = new Mustache(null, array('title' => 'Bear > Shark'));
$this->assertEquals('Bear > Shark', $m->render('{{%UNESCAPED}}{{title}}'));
$this->assertEquals('Bear &gt; Shark', $m->render('{{%UNESCAPED}}{{{title}}}'));
}
}
+133 -1
View File
@@ -34,6 +34,139 @@ require_once 'PHPUnit/Framework.php';
*/ */
class MustacheTest extends PHPUnit_Framework_TestCase { class MustacheTest extends PHPUnit_Framework_TestCase {
const TEST_CLASS = 'Mustache';
/**
* Test Mustache constructor.
*
* @access public
* @return void
*/
public function test__construct() {
$template = '{{#mustaches}}{{#last}}and {{/last}}{{type}}{{^last}}, {{/last}}{{/mustaches}}';
$data = array(
'mustaches' => array(
array('type' => 'Natural'),
array('type' => 'Hungarian'),
array('type' => 'Dali'),
array('type' => 'English'),
array('type' => 'Imperial'),
array('type' => 'Freestyle', 'last' => 'true'),
)
);
$output = 'Natural, Hungarian, Dali, English, Imperial, and Freestyle';
$m1 = new Mustache();
$this->assertEquals($output, $m1->render($template, $data));
$m2 = new Mustache($template);
$this->assertEquals($output, $m2->render(null, $data));
$m3 = new Mustache($template, $data);
$this->assertEquals($output, $m3->render());
$m4 = new Mustache(null, $data);
$this->assertEquals($output, $m4->render($template));
}
/**
* Test __toString() function.
*
* @access public
* @return void
*/
public function test__toString() {
$m = new Mustache('{{first_name}} {{last_name}}', array('first_name' => 'Karl', 'last_name' => 'Marx'));
$this->assertEquals('Karl Marx', $m->__toString());
$this->assertEquals('Karl Marx', (string) $m);
$m2 = $this->getMock(self::TEST_CLASS, array('render'), array());
$m2->expects($this->once())
->method('render')
->will($this->returnValue('foo'));
$this->assertEquals('foo', $m2->render());
}
public function test__toStringException() {
$m = $this->getMock(self::TEST_CLASS, array('render'), array());
$m->expects($this->once())
->method('render')
->will($this->throwException(new Exception));
try {
$out = (string) $m;
} catch (Exception $e) {
$this->fail('__toString should catch all exceptions');
}
}
/**
* Test render().
*
* @access public
* @return void
*/
public function testRender() {
$m = new Mustache();
$this->assertEquals('', $m->render(''));
$this->assertEquals('foo', $m->render('foo'));
$this->assertEquals('', $m->render(null));
$m2 = new Mustache('foo');
$this->assertEquals('foo', $m2->render());
$m3 = new Mustache('');
$this->assertEquals('', $m3->render());
$m3 = new Mustache();
$this->assertEquals('', $m3->render(null));
}
/**
* Test render() with data.
*
* @access public
* @return void
*/
public function testRenderWithData() {
$m = new Mustache('{{first_name}} {{last_name}}');
$this->assertEquals('Charlie Chaplin', $m->render(null, array('first_name' => 'Charlie', 'last_name' => 'Chaplin')));
$this->assertEquals('Zappa, Frank', $m->render('{{last_name}}, {{first_name}}', array('first_name' => 'Frank', 'last_name' => 'Zappa')));
}
/**
* Mustache should return the same thing when invoked multiple times.
*
* @access public
* @return void
*/
public function testMultipleInvocations() {
$m = new Mustache('x');
$first = $m->render();
$second = $m->render();
$this->assertEquals('x', $first);
$this->assertEquals($first, $second);
}
/**
* Mustache should return the same thing when invoked multiple times.
*
* @access public
* @return void
*/
public function testMultipleInvocationsWithTags() {
$m = new Mustache('{{one}} {{two}}', array('one' => 'foo', 'two' => 'bar'));
$first = $m->render();
$second = $m->render();
$this->assertEquals('foo bar', $first);
$this->assertEquals($first, $second);
}
/** /**
* Test everything in the `examples` directory. * Test everything in the `examples` directory.
* *
@@ -49,7 +182,6 @@ class MustacheTest extends PHPUnit_Framework_TestCase {
$this->assertEquals($output, $m->render($template)); $this->assertEquals($output, $m->render($template));
} }
/** /**
* Data provider for testExamples method. * Data provider for testExamples method.
* *