Wrap the render() function in a __toString method for implicit string conversion.

This commit is contained in:
Justin Hileman
2010-03-23 10:31:51 -04:00
parent 0586105b5e
commit b2666da4bb
+17
View File
@@ -71,6 +71,23 @@ class Mustache {
return $this->_render($template, $this->context); return $this->_render($template, $this->context);
} }
/**
* Wrap the render() function for string conversion.
*
* @access public
* @return string
*/
public function __toString() {
// PHP doesn't like exceptions in __toString.
// catch any exceptions and convert them to strings.
try {
$result = $this->render();
return $result;
} catch (Exception $e) {
return "Error rendering mustache: " . $e->getMessage();
}
}
/** /**
* Internal render function, used for recursive calls. * Internal render function, used for recursive calls.