Просмотр исходного кода

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

Justin Hileman 16 лет назад
Родитель
Сommit
b2666da4bb
1 измененных файлов с 17 добавлено и 0 удалено
  1. 17 0
      Mustache.php

+ 17 - 0
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.