Adding Handlebars_String to fix #12

If a helper return a Handlebars_String, then the result is compiled again.
This commit is contained in:
fzerorubigd 2013-06-01 17:38:47 +04:30
parent 63cd738e64
commit 65eb09d101
7 changed files with 109 additions and 14 deletions

View File

@ -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
*

View File

@ -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 <fzerorubigd@gmail.com>
@ -17,7 +17,7 @@
/**
* Handlebars loader interface
*
*
* @category Xamin
* @package Handlebars
* @author fzerorubigd <fzerorubigd@gmail.com>
@ -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);
}

View File

@ -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]);
}
/**

View File

@ -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 <fzerorubigd@gmail.com>
@ -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);
}
}

74
src/Handlebars/String.php Normal file
View File

@ -0,0 +1,74 @@
<?php
/**
* This file is part of Handlebars-php
*
* PHP version 5.3
*
* @category Xamin
* @package Handlebars
* @author fzerorubigd <fzerorubigd@gmail.com>
* @copyright 2013 Authors
* @license GPLv3 <http://www.gnu.org/licenses/gpl-3.0.html>
* @version GIT: $Id$
* @link http://xamin.ir
*/
/**
* Handlebars string
*
* @category Xamin
* @package Handlebars
* @author fzerorubigd <fzerorubigd@gmail.com>
* @copyright 2013 Authors
* @license GPLv3 <http://www.gnu.org/licenses/gpl-3.0.html>
* @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;
}
}

View File

@ -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
*

View File

@ -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)) {