move stop token into stack, fix #3

This commit is contained in:
fzerorubigd 2012-11-28 12:55:07 +03:30
parent d43d777ce6
commit 30795180a9

View File

@ -45,7 +45,6 @@ class Handlebars_Template
*/ */
private $_stack = array(); private $_stack = array();
private $_stopToken = false;
/** /**
* Handlebars template constructor * Handlebars template constructor
* *
@ -58,7 +57,7 @@ class Handlebars_Template
$this->handlebars = $engine; $this->handlebars = $engine;
$this->tree = $tree; $this->tree = $tree;
$this->source = $source; $this->source = $source;
array_push($this->_stack, array (0, $this->getTree())); array_push($this->_stack, array (0, $this->getTree(), false));
} }
/** /**
@ -101,7 +100,9 @@ class Handlebars_Template
public function setStopToken($token) public function setStopToken($token)
{ {
$this->_stopToken = $token; $topStack = array_pop($this->_stack);
$topStack[2] = $token;
array_push($this->_stack, $topStack);
} }
/** /**
@ -112,7 +113,8 @@ class Handlebars_Template
public function getStopToken() public function getStopToken()
{ {
return $this->_stopToken; $topStack = end($this->_stack);
return $topStack[2];
} }
/** /**
* Render top tree * Render top tree
@ -126,24 +128,27 @@ 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 (render) never pop a value from stack
list($index ,$tree) = $topTree; if (count($topTree) < 3) {
print_r($topTree);die();
}
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($this->_stopToken) if (is_string($stop)
&& $current[Handlebars_Tokenizer::TYPE] == Handlebars_Tokenizer::T_ESCAPED && $current[Handlebars_Tokenizer::TYPE] == Handlebars_Tokenizer::T_ESCAPED
&& $current[Handlebars_Tokenizer::NAME] === $this->_stopToken && $current[Handlebars_Tokenizer::NAME] === $stop
) { ) {
break; break;
} }
switch ($current[Handlebars_Tokenizer::TYPE]) { switch ($current[Handlebars_Tokenizer::TYPE]) {
case Handlebars_Tokenizer::T_SECTION : case Handlebars_Tokenizer::T_SECTION :
$newStack = isset($current[Handlebars_Tokenizer::NODES]) ? $current[Handlebars_Tokenizer::NODES] : array(); $newStack = isset($current[Handlebars_Tokenizer::NODES]) ? $current[Handlebars_Tokenizer::NODES] : array();
array_push($this->_stack, array(0, $newStack)); array_push($this->_stack, array(0, $newStack, false));
$buffer .= $this->_section($context, $current); $buffer .= $this->_section($context, $current);
array_pop($this->_stack); array_pop($this->_stack);
break; break;
@ -168,10 +173,11 @@ class Handlebars_Template
throw new RuntimeException('Invalid node type : ' . json_encode($current)); throw new RuntimeException('Invalid node type : ' . json_encode($current));
} }
} }
if ($this->_stopToken) { if ($stop) {
//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[2] = false; //No stop token from now on
array_push($this->_stack, $newStack); array_push($this->_stack, $newStack);
} }
return $buffer; return $buffer;
@ -190,23 +196,24 @@ class Handlebars_Template
$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) = $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($this->_stopToken) if (is_string($stop)
&& $current[Handlebars_Tokenizer::TYPE] == Handlebars_Tokenizer::T_ESCAPED && $current[Handlebars_Tokenizer::TYPE] == Handlebars_Tokenizer::T_ESCAPED
&& $current[Handlebars_Tokenizer::NAME] === $this->_stopToken && $current[Handlebars_Tokenizer::NAME] === $stop
) { ) {
break; break;
} }
} }
if ($this->_stopToken) { if ($stop) {
//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;
array_push($this->_stack, $newStack); array_push($this->_stack, $newStack);
} }
return ''; return '';