Added MustacheException class, added exceptions for missing variables and partials and unclosed or unopened section tags. Added throwExceptions member variable to enable/disable throwing exceptions on unexpected tags. Defaults to false.
This commit is contained in:
+54
-1
@@ -16,6 +16,10 @@ class Mustache {
|
||||
|
||||
public $otag = '{{';
|
||||
public $ctag = '}}';
|
||||
|
||||
// Should this Mustache throw exceptions when it finds unexpected tags?
|
||||
protected $throwExceptions = false;
|
||||
|
||||
protected $tagRegEx;
|
||||
|
||||
protected $template = '';
|
||||
@@ -134,7 +138,7 @@ class Mustache {
|
||||
|
||||
$otag = $this->prepareRegEx($this->otag);
|
||||
$ctag = $this->prepareRegEx($this->ctag);
|
||||
$this->tagRegEx = '/' . $otag . "(=|!|>|\\{|&)?([^\/#]+?)\\1?" . $ctag . "+/";
|
||||
$this->tagRegEx = '/' . $otag . "(#|\/|=|!|>|\\{|&)?([^\/#]+?)\\1?" . $ctag . "+/";
|
||||
$html = '';
|
||||
$matches = array();
|
||||
while (preg_match($this->tagRegEx, $template, $matches, PREG_OFFSET_CAPTURE)) {
|
||||
@@ -165,6 +169,20 @@ class Mustache {
|
||||
*/
|
||||
protected function renderTag($modifier, $tag_name, &$context) {
|
||||
switch ($modifier) {
|
||||
case '#':
|
||||
if ($this->throwExceptions) {
|
||||
throw new MustacheException('Unclosed section: ' . $tag_name, MustacheException::UNCLOSED_SECTION);
|
||||
} else {
|
||||
return '';
|
||||
}
|
||||
break;
|
||||
case '/':
|
||||
if ($this->throwExceptions) {
|
||||
throw new MustacheException('Unexpected close section: ' . $tag_name, MustacheException::UNEXPECTED_CLOSE_SECTION);
|
||||
} else {
|
||||
return '';
|
||||
}
|
||||
break;
|
||||
case '=':
|
||||
return $this->changeDelimiter($tag_name, $context);
|
||||
break;
|
||||
@@ -302,8 +320,13 @@ class Mustache {
|
||||
return $view[$tag_name];
|
||||
}
|
||||
}
|
||||
|
||||
if ($this->throwExceptions) {
|
||||
throw new MustacheException("Unknown variable: " . $tag_name, MustacheException::UNKNOWN_VARIABLE);
|
||||
} else {
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the partial corresponding to the requested tag name.
|
||||
@@ -318,8 +341,13 @@ class Mustache {
|
||||
if (is_array($this->partials) && isset($this->partials[$tag_name])) {
|
||||
return $this->partials[$tag_name];
|
||||
}
|
||||
|
||||
if ($this->throwExceptions) {
|
||||
throw new MustacheException('Unknown partial: ' . $tag_name, MustacheException::UNKNOWN_PARTIAL);
|
||||
} else {
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare a string to be used in a regular expression.
|
||||
@@ -337,3 +365,28 @@ class Mustache {
|
||||
return strtr($str, $replace);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* MustacheException class.
|
||||
*
|
||||
* @extends Exception
|
||||
*/
|
||||
class MustacheException extends Exception {
|
||||
|
||||
// An UNKNOWN_VARIABLE exception is thrown when a {{variable}} is not found
|
||||
// in the current context.
|
||||
const UNKNOWN_VARIABLE = 0;
|
||||
|
||||
// An UNCLOSED_SECTION exception is thrown when a {{#section}} is not closed.
|
||||
const UNCLOSED_SECTION = 1;
|
||||
|
||||
// An UNEXPECTED_CLOSE_SECTION exception is thrown when {{/section}} appears
|
||||
// without a corresponding {{#section}}.
|
||||
const UNEXPECTED_CLOSE_SECTION = 2;
|
||||
|
||||
// An UNKNOWN_PARTIAL exception is thrown whenever a {{>partial}} tag appears
|
||||
// with no associated partial.
|
||||
const UNKNOWN_PARTIAL = 3;
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user