From b2666da4bb023a1c808d10781235ab48cccdb646 Mon Sep 17 00:00:00 2001 From: Justin Hileman Date: Tue, 23 Mar 2010 10:31:51 -0400 Subject: [PATCH] Wrap the render() function in a __toString method for implicit string conversion. --- Mustache.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/Mustache.php b/Mustache.php index d652a19..838d21b 100644 --- a/Mustache.php +++ b/Mustache.php @@ -71,6 +71,23 @@ class Mustache { 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.