diff --git a/src/Handlebars/Engine.php b/src/Handlebars/Engine.php index a4ddadd..64056eb 100644 --- a/src/Handlebars/Engine.php +++ b/src/Handlebars/Engine.php @@ -418,6 +418,20 @@ class Handlebars_Engine return new Handlebars_Template($this, $tree, $source); } + + /** + * Load string into a template object + * + * @param string $source string to load + * + * @return Handlebars_Template + */ + public function loadString($source) + { + $tree = $this->_tokenize($source); + return new Handlebars_Template($this, $tree, $source); + } + /** * try to tokenize source, or get them from cache if available * diff --git a/src/Handlebars/Loader.php b/src/Handlebars/Loader.php index b13e9f4..91c7c0e 100644 --- a/src/Handlebars/Loader.php +++ b/src/Handlebars/Loader.php @@ -2,9 +2,9 @@ /** * This file is part of Handlebars-php * Base on mustache-php https://github.com/bobthecow/mustache.php - * + * * PHP version 5.3 - * + * * @category Xamin * @package Handlebars * @author fzerorubigd @@ -17,7 +17,7 @@ /** * Handlebars loader interface - * + * * @category Xamin * @package Handlebars * @author fzerorubigd @@ -34,7 +34,7 @@ interface Handlebars_Loader * * @param string $name template name to load * - * @return string Mustache Template source + * @return Handlebars_String */ public function load($name); } diff --git a/src/Handlebars/Loader/FilesystemLoader.php b/src/Handlebars/Loader/FilesystemLoader.php index 677bc58..93c9212 100644 --- a/src/Handlebars/Loader/FilesystemLoader.php +++ b/src/Handlebars/Loader/FilesystemLoader.php @@ -84,15 +84,14 @@ class Handlebars_Loader_FilesystemLoader implements Handlebars_Loader * * @param string $name template name * - * @return string Handlebars Template source + * @return Handlebars_String Handlebars Template source */ public function load($name) { if (!isset($this->_templates[$name])) { $this->_templates[$name] = $this->loadFile($name); } - - return $this->_templates[$name]; + return new Handlebars_String($this->_templates[$name]); } /** diff --git a/src/Handlebars/Loader/StringLoader.php b/src/Handlebars/Loader/StringLoader.php index 98cd2ae..def3f8a 100644 --- a/src/Handlebars/Loader/StringLoader.php +++ b/src/Handlebars/Loader/StringLoader.php @@ -2,9 +2,9 @@ /** * This file is part of Handlebars-php * Base on mustache-php https://github.com/bobthecow/mustache.php - * + * * PHP version 5.3 - * + * * @category Xamin * @package Handlebars * @author fzerorubigd @@ -34,10 +34,10 @@ class Handlebars_Loader_StringLoader implements Handlebars_Loader * * @param string $name Handlebars Template source * - * @return string Handlebars Template source + * @return Handlebars_string Handlebars Template source */ public function load($name) { - return $name; + return new Handlebars_String($name); } } diff --git a/src/Handlebars/String.php b/src/Handlebars/String.php new file mode 100644 index 0000000..3cb321e --- /dev/null +++ b/src/Handlebars/String.php @@ -0,0 +1,74 @@ + + * @copyright 2013 Authors + * @license GPLv3 + * @version GIT: $Id$ + * @link http://xamin.ir + */ + + +/** + * Handlebars string + * + * @category Xamin + * @package Handlebars + * @author fzerorubigd + * @copyright 2013 Authors + * @license GPLv3 + * @version Release: @package_version@ + * @link http://xamin.ir + */ + +class Handlebars_String +{ + 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 2170772..6f56259 100644 --- a/src/Handlebars/Template.php +++ b/src/Handlebars/Template.php @@ -154,7 +154,7 @@ class Handlebars_Template array_push($this->_stack, array(0, $newStack, false)); $buffer .= $this->_inverted($context, $current); array_pop($this->_stack); - break; + break; case Handlebars_Tokenizer::T_COMMENT : $buffer .= ''; break; @@ -249,7 +249,12 @@ class Handlebars_Template $current[Handlebars_Tokenizer::ARGS], //Arguments $source ); - return call_user_func_array($helpers->$sectionName, $params); + $return = call_user_func_array($helpers->$sectionName, $params); + if ($return instanceof Handlebars_String) { + return $this->handlebars->loadString($return)->render($context); + } else { + return $return; + } } elseif (trim($current[Handlebars_Tokenizer::ARGS]) == '') { //Fallback for mustache style each/with/for just if there is no argument at all. try { @@ -297,7 +302,7 @@ class Handlebars_Template return ''; } } - + /** * Process partial section * diff --git a/src/Handlebars/Tokenizer.php b/src/Handlebars/Tokenizer.php index b8c4442..4179a06 100644 --- a/src/Handlebars/Tokenizer.php +++ b/src/Handlebars/Tokenizer.php @@ -106,6 +106,9 @@ class Handlebars_Tokenizer */ public function scan($text, $delimiters = null) { + if ($text instanceof Handlebars_String) { + $text = $text->getString(); + } $this->reset(); if ($delimiters = trim($delimiters)) {