From 79373a193a32cd442bd3617f30e2d7167ac86887 Mon Sep 17 00:00:00 2001 From: Dmitriy Simushev Date: Mon, 3 Feb 2014 08:35:17 +0000 Subject: [PATCH] Add \Handlebars\SafeString class --- src/Handlebars/BaseString.php | 78 ++++++++++++++++++++++++++++++++++ src/Handlebars/SafeString.php | 33 ++++++++++++++ src/Handlebars/String.php | 49 ++------------------- src/Handlebars/Template.php | 2 +- tests/Xamin/HandlebarsTest.php | 5 +++ 5 files changed, 120 insertions(+), 47 deletions(-) create mode 100644 src/Handlebars/BaseString.php create mode 100644 src/Handlebars/SafeString.php mode change 100755 => 100644 src/Handlebars/String.php diff --git a/src/Handlebars/BaseString.php b/src/Handlebars/BaseString.php new file mode 100644 index 0000000..1153959 --- /dev/null +++ b/src/Handlebars/BaseString.php @@ -0,0 +1,78 @@ + + * @author Behrooz Shabani + * @author Dmitriy Simushev + * @copyright 2013 Authors + * @license MIT + * @version GIT: $Id$ + * @link http://xamin.ir + */ + +namespace Handlebars; + +/** + * Handlebars base string + * + * @category Xamin + * @package Handlebars + * @author fzerorubigd + * @copyright 2013 Authors + * @license MIT + * @version Release: @package_version@ + * @link http://xamin.ir + */ + +class BaseString +{ + private $_string; + + /** + * Create new string + * + * @param string $string input source + */ + public function __construct($string) + { + $this->_string = $string; + } + + /** + * To String + * + * @return string + */ + public function __toString() + { + return $this->getString(); + } + + /** + * Get string + * + * @return string + */ + public function getString() + { + return $this->_string; + } + + /** + * Create new string + * + * @param string $string input source + * + * @return void + */ + public function setString($string) + { + $this->_string = $string; + } + +} diff --git a/src/Handlebars/SafeString.php b/src/Handlebars/SafeString.php new file mode 100644 index 0000000..6332194 --- /dev/null +++ b/src/Handlebars/SafeString.php @@ -0,0 +1,33 @@ + + * @copyright 2014 Authors + * @license MIT + * @version GIT: $Id$ + * @link http://xamin.ir + */ + +namespace Handlebars; + +/** + * Handlebars safe string. Can be used in line helpers as wrapper for result to + * indicate that there is no need to escape the result. + * + * @category Xamin + * @package Handlebars + * @author Dmitriy Simushev + * @copyright 2014 Authors + * @license MIT + * @version Release: @package_version@ + * @link http://xamin.ir + */ + +class SafeString extends BaseString +{ +} diff --git a/src/Handlebars/String.php b/src/Handlebars/String.php old mode 100755 new mode 100644 index 36c174e..b1b0d37 --- a/src/Handlebars/String.php +++ b/src/Handlebars/String.php @@ -8,6 +8,7 @@ * @package Handlebars * @author fzerorubigd * @author Behrooz Shabani + * @author Dmitriy Simushev * @copyright 2013 Authors * @license MIT * @version GIT: $Id$ @@ -28,50 +29,6 @@ * @link http://xamin.ir */ -class String +class String extends BaseString { - private $_string; - - /** - * Create new string - * - * @param string $string input source - */ - public function __construct($string) - { - $this->_string = $string; - } - - /** - * To String - * - * @return string - */ - public function __toString() - { - return $this->getString(); - } - - /** - * Get string - * - * @return string - */ - public function getString() - { - return $this->_string; - } - - /** - * Create new string - * - * @param string $string input source - * - * @return void - */ - public function setString($string) - { - $this->_string = $string; - } - -} \ No newline at end of file +} diff --git a/src/Handlebars/Template.php b/src/Handlebars/Template.php index d88165b..e28b220 100644 --- a/src/Handlebars/Template.php +++ b/src/Handlebars/Template.php @@ -408,7 +408,7 @@ private function _getSection(Context $context, $current, $escaped) $current[Tokenizer::ARGS] = implode(' ', $args); $result = $this->_section($context, $current); - if ($escaped) { + if ($escaped && !($result instanceof SafeString)) { $escape_args = $this->handlebars->getEscapeArgs(); array_unshift($escape_args, $result); $result = call_user_func_array( diff --git a/tests/Xamin/HandlebarsTest.php b/tests/Xamin/HandlebarsTest.php index 9cb162d..87c11d6 100644 --- a/tests/Xamin/HandlebarsTest.php +++ b/tests/Xamin/HandlebarsTest.php @@ -246,6 +246,11 @@ public function testCustomHelper() }); $this->assertEquals('Test', $engine->render('{{{markupHelper}}}', array())); $this->assertEquals('<strong>Test</strong>', $engine->render('{{markupHelper}}', array())); + + $engine->addHelper('safeStringTest', function() { + return new \Handlebars\SafeString('Test'); + }); + $this->assertEquals('Test', $engine->render('{{safeStringTest}}', array())); } /**