From 23076d68e5339640cab7b748dc9834f160c09738 Mon Sep 17 00:00:00 2001 From: Martin Zeman Date: Sun, 7 Jun 2015 23:09:14 +0200 Subject: [PATCH] Added support for class with __toString method as a context variable. --- src/Handlebars/Template.php | 4 ++-- tests/Xamin/HandlebarsTest.php | 11 ++++++++++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/Handlebars/Template.php b/src/Handlebars/Template.php index 45bdd37..275e547 100644 --- a/src/Handlebars/Template.php +++ b/src/Handlebars/Template.php @@ -591,14 +591,14 @@ class Template } if ($escaped) { $args = $this->handlebars->getEscapeArgs(); - array_unshift($args, $value); + array_unshift($args, (string)$value); $value = call_user_func_array( $this->handlebars->getEscape(), array_values($args) ); } - return $value; + return (string)$value; } /** diff --git a/tests/Xamin/HandlebarsTest.php b/tests/Xamin/HandlebarsTest.php index 517549c..fe5a5c1 100644 --- a/tests/Xamin/HandlebarsTest.php +++ b/tests/Xamin/HandlebarsTest.php @@ -757,6 +757,9 @@ EOM; // Reference array as string $this->assertEquals('Array', $engine->render('{{var}}', array('var' => array('test')))); + // Test class with __toString method + $this->assertEquals('test', $engine->render('{{var}}', array('var' => new TestClassWithToStringMethod()))); + $obj = new DateTime(); $time = $obj->getTimestamp(); $this->assertEquals($time, $engine->render('{{time.getTimestamp}}', array('time' => $obj))); @@ -1270,6 +1273,12 @@ EOM; } +class TestClassWithToStringMethod { + public function __toString() { + return 'test'; + } +} + /** * Testcase for testInlineLoader * @@ -1282,4 +1291,4 @@ This is a inline template. a b c -d \ No newline at end of file +d