fix #4 , NEED TO ADD SOME TESTS ASAP

This commit is contained in:
fzerorubigd 2012-11-29 10:53:03 +03:30
parent 30795180a9
commit ecc1b11f2a

View File

@ -2,9 +2,9 @@
/** /**
* This file is part of Handlebars-php * This file is part of Handlebars-php
* Base on mustache-php https://github.com/bobthecow/mustache.php * Base on mustache-php https://github.com/bobthecow/mustache.php
* *
* PHP version 5.3 * PHP version 5.3
* *
* @category Xamin * @category Xamin
* @package Handlebars * @package Handlebars
* @author fzerorubigd <fzerorubigd@gmail.com> * @author fzerorubigd <fzerorubigd@gmail.com>
@ -18,7 +18,7 @@
/** /**
* Handlebars base template * Handlebars base template
* contain some utility method to get context and helpers * contain some utility method to get context and helpers
* *
* @category Xamin * @category Xamin
* @package Handlebars * @package Handlebars
* @author fzerorubigd <fzerorubigd@gmail.com> * @author fzerorubigd <fzerorubigd@gmail.com>
@ -34,7 +34,7 @@ class Handlebars_Template
* @var Handlebars_Engine * @var Handlebars_Engine
*/ */
protected $handlebars; protected $handlebars;
protected $tree = array(); protected $tree = array();
@ -82,40 +82,40 @@ class Handlebars_Template
/** /**
* Get current engine associated with this object * Get current engine associated with this object
* *
* @return Handlebars_Engine * @return Handlebars_Engine
*/ */
public function getEngine() public function getEngine()
{ {
return $this->handlebars; return $this->handlebars;
} }
/** /**
* set stop token for render and discard method * set stop token for render and discard method
* *
* @param string $token token to set as stop token or false to remove * @param string $token token to set as stop token or false to remove
* *
* @return void * @return void
*/ */
public function setStopToken($token) public function setStopToken($token)
{ {
$topStack = array_pop($this->_stack); $topStack = array_pop($this->_stack);
$topStack[2] = $token; $topStack[2] = $token;
array_push($this->_stack, $topStack); array_push($this->_stack, $topStack);
} }
/** /**
* get current stop token * get current stop token
* *
* @return string|false * @return string|false
*/ */
public function getStopToken() public function getStopToken()
{ {
$topStack = end($this->_stack); $topStack = end($this->_stack);
return $topStack[2]; return $topStack[2];
} }
/** /**
* Render top tree * Render top tree
* *
@ -127,20 +127,17 @@ class Handlebars_Template
{ {
if (!$context instanceof Handlebars_Context) { if (!$context instanceof Handlebars_Context) {
$context = new Handlebars_Context($context); $context = new Handlebars_Context($context);
} }
$topTree = end($this->_stack); //This method (render) never pop a value from stack $topTree = end($this->_stack); //This method (render) never pop a value from stack
if (count($topTree) < 3) {
print_r($topTree);die();
}
list($index ,$tree, $stop) = $topTree; list($index ,$tree, $stop) = $topTree;
$buffer = ''; $buffer = '';
while (array_key_exists($index, $tree)) { while (array_key_exists($index, $tree)) {
$current = $tree[$index]; $current = $tree[$index];
$index++; $index++;
//if the section is exactly like waitFor //if the section is exactly like waitFor
if (is_string($stop) if (is_string($stop)
&& $current[Handlebars_Tokenizer::TYPE] == Handlebars_Tokenizer::T_ESCAPED && $current[Handlebars_Tokenizer::TYPE] == Handlebars_Tokenizer::T_ESCAPED
&& $current[Handlebars_Tokenizer::NAME] === $stop && $current[Handlebars_Tokenizer::NAME] === $stop
) { ) {
break; break;
@ -152,7 +149,7 @@ class Handlebars_Template
$buffer .= $this->_section($context, $current); $buffer .= $this->_section($context, $current);
array_pop($this->_stack); array_pop($this->_stack);
break; break;
case Handlebars_Tokenizer::T_COMMENT : case Handlebars_Tokenizer::T_COMMENT :
$buffer .= ''; $buffer .= '';
break; break;
case Handlebars_Tokenizer::T_PARTIAL: case Handlebars_Tokenizer::T_PARTIAL:
@ -178,11 +175,11 @@ class Handlebars_Template
$newStack = array_pop($this->_stack); $newStack = array_pop($this->_stack);
$newStack[0] = $index; $newStack[0] = $index;
$newStack[2] = false; //No stop token from now on $newStack[2] = false; //No stop token from now on
array_push($this->_stack, $newStack); array_push($this->_stack, $newStack);
} }
return $buffer; return $buffer;
} }
/** /**
* Discard top tree * Discard top tree
* *
@ -194,16 +191,15 @@ class Handlebars_Template
{ {
if (!$context instanceof Handlebars_Context) { if (!$context instanceof Handlebars_Context) {
$context = new Handlebars_Context($context); $context = new Handlebars_Context($context);
} }
$topTree = end($this->_stack); //This method never pop a value from stack $topTree = end($this->_stack); //This method never pop a value from stack
list($index ,$tree, $stop) = $topTree; list($index ,$tree, $stop) = $topTree;
while (array_key_exists($index, $tree)) { while (array_key_exists($index, $tree)) {
$current = $tree[$index]; $current = $tree[$index];
$index++; $index++;
//if the section is exactly like waitFor //if the section is exactly like waitFor
if (is_string($stop) if (is_string($stop)
&& $current[Handlebars_Tokenizer::TYPE] == Handlebars_Tokenizer::T_ESCAPED && $current[Handlebars_Tokenizer::TYPE] == Handlebars_Tokenizer::T_ESCAPED
&& $current[Handlebars_Tokenizer::NAME] === $stop && $current[Handlebars_Tokenizer::NAME] === $stop
) { ) {
break; break;
@ -213,11 +209,11 @@ class Handlebars_Template
//Ok break here, the helper should be aware of this. //Ok break here, the helper should be aware of this.
$newStack = array_pop($this->_stack); $newStack = array_pop($this->_stack);
$newStack[0] = $index; $newStack[0] = $index;
$newStack[0] = false; $newStack[2] = false;
array_push($this->_stack, $newStack); array_push($this->_stack, $newStack);
} }
return ''; return '';
} }
/** /**
* Process section nodes * Process section nodes
@ -226,7 +222,7 @@ class Handlebars_Template
* @param array $current section node data * @param array $current section node data
* *
* @return string the result * @return string the result
*/ */
private function _section(Handlebars_Context $context, $current) private function _section(Handlebars_Context $context, $current)
{ {
$helpers = $this->handlebars->getHelpers(); $helpers = $this->handlebars->getHelpers();
@ -240,7 +236,7 @@ class Handlebars_Template
); );
} else { } else {
$source = ''; $source = '';
} }
$params = array( $params = array(
$this, //First argument is this template $this, //First argument is this template
$context, //Secound is current context $context, //Secound is current context
@ -248,12 +244,12 @@ class Handlebars_Template
$source $source
); );
return call_user_func_array($helpers->$sectionName, $params); return call_user_func_array($helpers->$sectionName, $params);
} elseif (trim($current[Handlebars_Tokenizer::ARGS]) == '') { } elseif (trim($current[Handlebars_Tokenizer::ARGS]) == '') {
//Fallback for mustache style each/with/for just if there is no argument at all. //Fallback for mustache style each/with/for just if there is no argument at all.
try { try {
$sectionVar = $context->get($sectionName, true); $sectionVar = $context->get($sectionName, true);
} catch (InvalidArgumentException $e) { } catch (InvalidArgumentException $e) {
throw new RuntimeException($sectionName . ' is not registered as a helper'); throw new RuntimeException($sectionName . ' is not registered as a helper');
} }
$buffer = ''; $buffer = '';
if (is_array($sectionVar) || $sectionVar instanceof Traversable) { if (is_array($sectionVar) || $sectionVar instanceof Traversable) {
@ -263,7 +259,7 @@ class Handlebars_Template
$context->pop(); $context->pop();
} }
} elseif (is_object($sectionVar)) { } elseif (is_object($sectionVar)) {
//Act like with //Act like with
$context->push($sectionVar); $context->push($sectionVar);
$buffer = $this->render($context); $buffer = $this->render($context);
$context->pop(); $context->pop();
@ -273,7 +269,7 @@ class Handlebars_Template
return $buffer; return $buffer;
} else { } else {
throw new RuntimeException($sectionName . ' is not registered as a helper'); throw new RuntimeException($sectionName . ' is not registered as a helper');
} }
} }
/** /**
@ -309,6 +305,6 @@ class Handlebars_Template
} }
return $value; return $value;
} }
} }