mirror of
https://github.com/Mibew/handlebars.php.git
synced 2025-04-17 19:57:24 +03:00
fix #4 , NEED TO ADD SOME TESTS ASAP
This commit is contained in:
parent
30795180a9
commit
ecc1b11f2a
@ -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>
|
||||
@ -18,7 +18,7 @@
|
||||
/**
|
||||
* Handlebars base template
|
||||
* contain some utility method to get context and helpers
|
||||
*
|
||||
*
|
||||
* @category Xamin
|
||||
* @package Handlebars
|
||||
* @author fzerorubigd <fzerorubigd@gmail.com>
|
||||
@ -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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user