Prefix Mustache reserved member variables with underscore to prevent namespace collisions with actual view values.

This commit is contained in:
Justin Hileman
2010-04-30 20:12:37 -04:00
parent ac365c052e
commit 1532416306
+44 -44
View File
@@ -14,8 +14,8 @@
*/ */
class Mustache { class Mustache {
public $otag = '{{'; public $_otag = '{{';
public $ctag = '}}'; public $_ctag = '}}';
// Should this Mustache throw exceptions when it finds unexpected tags? // Should this Mustache throw exceptions when it finds unexpected tags?
protected $throwSectionExceptions = true; protected $throwSectionExceptions = true;
@@ -23,18 +23,18 @@ class Mustache {
protected $throwVariableExceptions = false; protected $throwVariableExceptions = false;
// 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';
protected $tagRegEx; protected $_tagRegEx;
protected $template = ''; protected $_template = '';
protected $context = array(); protected $_context = array();
protected $partials = array(); protected $_partials = array();
protected $pragmas = array(); protected $_pragmas = array();
protected $pragmasImplemented = array( protected $_pragmasImplemented = array(
self::PRAGMA_DOT_NOTATION self::PRAGMA_DOT_NOTATION
); );
@@ -51,9 +51,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);
} }
/** /**
@@ -69,16 +69,16 @@ 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); return $this->_render($template, $this->_context);
} }
/** /**
@@ -121,8 +121,8 @@ class Mustache {
* @return string * @return string
*/ */
protected function renderSection($template, &$context) { protected function renderSection($template, &$context) {
$otag = $this->prepareRegEx($this->otag); $otag = $this->prepareRegEx($this->_otag);
$ctag = $this->prepareRegEx($this->ctag); $ctag = $this->prepareRegEx($this->_ctag);
$regex = '/' . $otag . '(\\^|\\#)(.+?)' . $ctag . '\\s*([\\s\\S]+?)' . $otag . '\\/\\2' . $ctag . '\\s*/m'; $regex = '/' . $otag . '(\\^|\\#)(.+?)' . $ctag . '\\s*([\\s\\S]+?)' . $otag . '\\/\\2' . $ctag . '\\s*/m';
$matches = array(); $matches = array();
@@ -175,12 +175,12 @@ class Mustache {
*/ */
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 . '%([\\w_-]+)((?: [\\w]+=[\\w]+)*)' . $ctag . '\\n?/';
return preg_replace_callback($regex, array($this, 'renderPragma'), $template); return preg_replace_callback($regex, array($this, 'renderPragma'), $template);
} }
@@ -198,7 +198,7 @@ class Mustache {
$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);
} }
@@ -211,9 +211,9 @@ 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 '';
@@ -227,7 +227,7 @@ class Mustache {
* @return bool * @return bool
*/ */
protected function hasPragma($pragma_name) { protected function hasPragma($pragma_name) {
if (array_key_exists($pragma_name, $this->pragmas) && $this->pragmas[$pragma_name]) { if (array_key_exists($pragma_name, $this->_pragmas) && $this->_pragmas[$pragma_name]) {
return true; return true;
} else { } else {
return false; return false;
@@ -247,7 +247,7 @@ class Mustache {
throw new MustacheException('Unknown pragma: ' . $pragma_name, MustacheException::UNKNOWN_PRAGMA); throw new MustacheException('Unknown pragma: ' . $pragma_name, MustacheException::UNKNOWN_PRAGMA);
} }
return $this->pragmas[$pragma_name]; return $this->_pragmas[$pragma_name];
} }
/** /**
@@ -259,18 +259,18 @@ class Mustache {
* @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];
@@ -343,7 +343,7 @@ class Mustache {
* @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);
} }
/** /**
@@ -379,9 +379,9 @@ class Mustache {
* @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), $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,12 +396,12 @@ class Mustache {
*/ */
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 '';
} }
@@ -498,8 +498,8 @@ class Mustache {
* @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->throwPartialExceptions) {