From 4244730d245d60f280ed21209640dc79c6ab269e Mon Sep 17 00:00:00 2001 From: fzerorubigd Date: Mon, 19 Nov 2012 17:35:38 +0330 Subject: [PATCH 1/4] fix a bug with stop token (when there is no stop token at all) --- src/Handlebars/Template.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Handlebars/Template.php b/src/Handlebars/Template.php index fc2cea6..54e7288 100644 --- a/src/Handlebars/Template.php +++ b/src/Handlebars/Template.php @@ -138,10 +138,6 @@ class Handlebars_Template && $current[Handlebars_Tokenizer::TYPE] == Handlebars_Tokenizer::T_ESCAPED && $current[Handlebars_Tokenizer::NAME] === $this->_stopToken ) { - //Ok break here, the helper should be aware of this. - $newStack = array_pop($this->_stack); - $newStack[0] = $index; - array_push($this->_stack, $newStack); break; } switch ($current[Handlebars_Tokenizer::TYPE]) { @@ -172,6 +168,10 @@ class Handlebars_Template throw new RuntimeException('Invalid node type : ' . json_encode($current)); } } + //Ok break here, the helper should be aware of this. + $newStack = array_pop($this->_stack); + $newStack[0] = $index; + array_push($this->_stack, $newStack); return $buffer; } @@ -198,13 +198,13 @@ class Handlebars_Template && $current[Handlebars_Tokenizer::TYPE] == Handlebars_Tokenizer::T_ESCAPED && $current[Handlebars_Tokenizer::NAME] === $this->_stopToken ) { - //Ok break here, the helper should be aware of this. - $newStack = array_pop($this->_stack); - $newStack[0] = $index; - array_push($this->_stack, $newStack); break; } } + //Ok break here, the helper should be aware of this. + $newStack = array_pop($this->_stack); + $newStack[0] = $index; + array_push($this->_stack, $newStack); return ''; } From d43d777ce6ec22d14b9019fede71ce169e47926f Mon Sep 17 00:00:00 2001 From: fzerorubigd Date: Mon, 19 Nov 2012 17:56:10 +0330 Subject: [PATCH 2/4] change stack position just if there is stop token available --- src/Handlebars/Template.php | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/Handlebars/Template.php b/src/Handlebars/Template.php index 54e7288..0ace75d 100644 --- a/src/Handlebars/Template.php +++ b/src/Handlebars/Template.php @@ -168,10 +168,12 @@ class Handlebars_Template throw new RuntimeException('Invalid node type : ' . json_encode($current)); } } - //Ok break here, the helper should be aware of this. - $newStack = array_pop($this->_stack); - $newStack[0] = $index; - array_push($this->_stack, $newStack); + if ($this->_stopToken) { + //Ok break here, the helper should be aware of this. + $newStack = array_pop($this->_stack); + $newStack[0] = $index; + array_push($this->_stack, $newStack); + } return $buffer; } @@ -201,10 +203,12 @@ class Handlebars_Template break; } } - //Ok break here, the helper should be aware of this. - $newStack = array_pop($this->_stack); - $newStack[0] = $index; - array_push($this->_stack, $newStack); + if ($this->_stopToken) { + //Ok break here, the helper should be aware of this. + $newStack = array_pop($this->_stack); + $newStack[0] = $index; + array_push($this->_stack, $newStack); + } return ''; } From 30795180a953ec826e22c0e82fab57c16d711fed Mon Sep 17 00:00:00 2001 From: fzerorubigd Date: Wed, 28 Nov 2012 12:55:07 +0330 Subject: [PATCH 3/4] move stop token into stack, fix #3 --- src/Handlebars/Template.php | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/src/Handlebars/Template.php b/src/Handlebars/Template.php index 0ace75d..2133a52 100644 --- a/src/Handlebars/Template.php +++ b/src/Handlebars/Template.php @@ -45,7 +45,6 @@ class Handlebars_Template */ private $_stack = array(); - private $_stopToken = false; /** * Handlebars template constructor * @@ -58,7 +57,7 @@ class Handlebars_Template $this->handlebars = $engine; $this->tree = $tree; $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) { - $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() { - return $this->_stopToken; + $topStack = end($this->_stack); + return $topStack[2]; } /** * Render top tree @@ -126,24 +128,27 @@ class Handlebars_Template if (!$context instanceof Handlebars_Context) { $context = new Handlebars_Context($context); } - $topTree = end($this->_stack); //This method never pop a value from stack - list($index ,$tree) = $topTree; + $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; $buffer = ''; while (array_key_exists($index, $tree)) { $current = $tree[$index]; $index++; //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::NAME] === $this->_stopToken + && $current[Handlebars_Tokenizer::NAME] === $stop ) { break; } switch ($current[Handlebars_Tokenizer::TYPE]) { case Handlebars_Tokenizer::T_SECTION : $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); array_pop($this->_stack); break; @@ -168,10 +173,11 @@ class Handlebars_Template throw new RuntimeException('Invalid node type : ' . json_encode($current)); } } - if ($this->_stopToken) { + if ($stop) { //Ok break here, the helper should be aware of this. $newStack = array_pop($this->_stack); $newStack[0] = $index; + $newStack[2] = false; //No stop token from now on array_push($this->_stack, $newStack); } return $buffer; @@ -190,23 +196,24 @@ class Handlebars_Template $context = new Handlebars_Context($context); } $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)) { $current = $tree[$index]; $index++; //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::NAME] === $this->_stopToken + && $current[Handlebars_Tokenizer::NAME] === $stop ) { break; } } - if ($this->_stopToken) { + if ($stop) { //Ok break here, the helper should be aware of this. $newStack = array_pop($this->_stack); $newStack[0] = $index; + $newStack[0] = false; array_push($this->_stack, $newStack); } return ''; From ecc1b11f2ad1019410e34de82ee203b0e4820788 Mon Sep 17 00:00:00 2001 From: fzerorubigd Date: Thu, 29 Nov 2012 10:53:03 +0330 Subject: [PATCH 4/4] fix #4 , NEED TO ADD SOME TESTS ASAP --- src/Handlebars/Template.php | 78 ++++++++++++++++++------------------- 1 file changed, 37 insertions(+), 41 deletions(-) diff --git a/src/Handlebars/Template.php b/src/Handlebars/Template.php index 2133a52..49e6a96 100644 --- a/src/Handlebars/Template.php +++ b/src/Handlebars/Template.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 @@ -18,7 +18,7 @@ /** * Handlebars base template * contain some utility method to get context and helpers - * + * * @category Xamin * @package Handlebars * @author fzerorubigd @@ -34,7 +34,7 @@ class Handlebars_Template * @var Handlebars_Engine */ protected $handlebars; - + protected $tree = array(); @@ -82,40 +82,40 @@ class Handlebars_Template /** * Get current engine associated with this object - * + * * @return Handlebars_Engine */ public function getEngine() { return $this->handlebars; } - + /** * set stop token for render and discard method * * @param string $token token to set as stop token or false to remove - * - * @return void + * + * @return void */ public function setStopToken($token) { - $topStack = array_pop($this->_stack); + $topStack = array_pop($this->_stack); $topStack[2] = $token; array_push($this->_stack, $topStack); } - + /** - * get current stop token + * get current stop token * - * @return string|false + * @return string|false */ public function getStopToken() { - $topStack = end($this->_stack); + $topStack = end($this->_stack); return $topStack[2]; - } + } /** * Render top tree * @@ -127,20 +127,17 @@ class Handlebars_Template { if (!$context instanceof Handlebars_Context) { $context = new Handlebars_Context($context); - } + } $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; $buffer = ''; while (array_key_exists($index, $tree)) { $current = $tree[$index]; $index++; - //if the section is exactly like waitFor - if (is_string($stop) - && $current[Handlebars_Tokenizer::TYPE] == Handlebars_Tokenizer::T_ESCAPED + //if the section is exactly like waitFor + if (is_string($stop) + && $current[Handlebars_Tokenizer::TYPE] == Handlebars_Tokenizer::T_ESCAPED && $current[Handlebars_Tokenizer::NAME] === $stop ) { break; @@ -152,7 +149,7 @@ class Handlebars_Template $buffer .= $this->_section($context, $current); array_pop($this->_stack); break; - case Handlebars_Tokenizer::T_COMMENT : + case Handlebars_Tokenizer::T_COMMENT : $buffer .= ''; break; case Handlebars_Tokenizer::T_PARTIAL: @@ -178,11 +175,11 @@ class Handlebars_Template $newStack = array_pop($this->_stack); $newStack[0] = $index; $newStack[2] = false; //No stop token from now on - array_push($this->_stack, $newStack); - } + array_push($this->_stack, $newStack); + } return $buffer; } - + /** * Discard top tree * @@ -194,16 +191,15 @@ class Handlebars_Template { if (!$context instanceof Handlebars_Context) { $context = new Handlebars_Context($context); - } + } $topTree = end($this->_stack); //This method never pop a value from stack list($index ,$tree, $stop) = $topTree; - while (array_key_exists($index, $tree)) { $current = $tree[$index]; $index++; - //if the section is exactly like waitFor - if (is_string($stop) - && $current[Handlebars_Tokenizer::TYPE] == Handlebars_Tokenizer::T_ESCAPED + //if the section is exactly like waitFor + if (is_string($stop) + && $current[Handlebars_Tokenizer::TYPE] == Handlebars_Tokenizer::T_ESCAPED && $current[Handlebars_Tokenizer::NAME] === $stop ) { break; @@ -213,11 +209,11 @@ class Handlebars_Template //Ok break here, the helper should be aware of this. $newStack = array_pop($this->_stack); $newStack[0] = $index; - $newStack[0] = false; - array_push($this->_stack, $newStack); + $newStack[2] = false; + array_push($this->_stack, $newStack); } return ''; - } + } /** * Process section nodes @@ -226,7 +222,7 @@ class Handlebars_Template * @param array $current section node data * * @return string the result - */ + */ private function _section(Handlebars_Context $context, $current) { $helpers = $this->handlebars->getHelpers(); @@ -240,7 +236,7 @@ class Handlebars_Template ); } else { $source = ''; - } + } $params = array( $this, //First argument is this template $context, //Secound is current context @@ -248,12 +244,12 @@ class Handlebars_Template $source ); 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. try { $sectionVar = $context->get($sectionName, true); } catch (InvalidArgumentException $e) { - throw new RuntimeException($sectionName . ' is not registered as a helper'); + throw new RuntimeException($sectionName . ' is not registered as a helper'); } $buffer = ''; if (is_array($sectionVar) || $sectionVar instanceof Traversable) { @@ -263,7 +259,7 @@ class Handlebars_Template $context->pop(); } } elseif (is_object($sectionVar)) { - //Act like with + //Act like with $context->push($sectionVar); $buffer = $this->render($context); $context->pop(); @@ -273,7 +269,7 @@ class Handlebars_Template return $buffer; } else { throw new RuntimeException($sectionName . ' is not registered as a helper'); - } + } } /** @@ -309,6 +305,6 @@ class Handlebars_Template } return $value; } - - + + }