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:
+56
-3
@@ -16,6 +16,10 @@ class Mustache {
|
|||||||
|
|
||||||
public $otag = '{{';
|
public $otag = '{{';
|
||||||
public $ctag = '}}';
|
public $ctag = '}}';
|
||||||
|
|
||||||
|
// Should this Mustache throw exceptions when it finds unexpected tags?
|
||||||
|
protected $throwExceptions = false;
|
||||||
|
|
||||||
protected $tagRegEx;
|
protected $tagRegEx;
|
||||||
|
|
||||||
protected $template = '';
|
protected $template = '';
|
||||||
@@ -134,7 +138,7 @@ class Mustache {
|
|||||||
|
|
||||||
$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)) {
|
||||||
@@ -165,6 +169,20 @@ class Mustache {
|
|||||||
*/
|
*/
|
||||||
protected function renderTag($modifier, $tag_name, &$context) {
|
protected function renderTag($modifier, $tag_name, &$context) {
|
||||||
switch ($modifier) {
|
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 '=':
|
case '=':
|
||||||
return $this->changeDelimiter($tag_name, $context);
|
return $this->changeDelimiter($tag_name, $context);
|
||||||
break;
|
break;
|
||||||
@@ -302,7 +320,12 @@ class Mustache {
|
|||||||
return $view[$tag_name];
|
return $view[$tag_name];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return '';
|
|
||||||
|
if ($this->throwExceptions) {
|
||||||
|
throw new MustacheException("Unknown variable: " . $tag_name, MustacheException::UNKNOWN_VARIABLE);
|
||||||
|
} else {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -318,7 +341,12 @@ class Mustache {
|
|||||||
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];
|
||||||
}
|
}
|
||||||
return '';
|
|
||||||
|
if ($this->throwExceptions) {
|
||||||
|
throw new MustacheException('Unknown partial: ' . $tag_name, MustacheException::UNKNOWN_PARTIAL);
|
||||||
|
} else {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -336,4 +364,29 @@ class Mustache {
|
|||||||
);
|
);
|
||||||
return strtr($str, $replace);
|
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