Added _throwsException() check and refactored exception checks.

This commit is contained in:
Justin Hileman
2010-04-30 20:23:25 -04:00
parent ab95bcd623
commit 5a1143efce
+30 -8
View File
@@ -17,10 +17,18 @@ 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';
@@ -265,6 +273,20 @@ class Mustache {
return $this->_pragmas[$pragma_name]; return $this->_pragmas[$pragma_name];
} }
/**
* 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]);
}
/** /**
* Loop through and render individual Mustache tags. * Loop through and render individual Mustache tags.
* *
@@ -316,14 +338,14 @@ class Mustache {
switch ($modifier) { switch ($modifier) {
case '#': case '#':
case '^': case '^':
if ($this->throwSectionExceptions) { 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 '';
@@ -503,7 +525,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 '';
@@ -525,7 +547,7 @@ class Mustache {
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 '';