change the way to fix #2

This commit is contained in:
fzerorubigd 2012-11-17 16:30:25 +03:30
parent ec6761fde4
commit b9b63836c7
2 changed files with 56 additions and 28 deletions

View File

@ -71,11 +71,16 @@ class Handlebars_Helpers
function ($template, $context, $args, $source) { function ($template, $context, $args, $source) {
$tmp = $context->get($args); $tmp = $context->get($args);
$buffer = ''; $buffer = '';
if ($tmp) { if ($tmp) {
$buffer = $template->render($context, 'else'); $template->setStopToken('else');
$buffer = $template->render($context);
$template->setStopToken(false);
$template->discard($context); $template->discard($context);
} else { } else {
$template->setStopToken('else');
$template->discard($context, 'else'); $template->discard($context, 'else');
$template->setStopToken(false);
$buffer = $template->render($context); $buffer = $template->render($context);
} }
return $buffer; return $buffer;

View File

@ -44,6 +44,8 @@ class Handlebars_Template
* @var array Run stack * @var array Run stack
*/ */
private $_stack = array(); private $_stack = array();
private $_stopToken = false;
/** /**
* Handlebars template constructor * Handlebars template constructor
* *
@ -89,15 +91,37 @@ class Handlebars_Template
return $this->handlebars; 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
*/
public function setStopToken($token)
{
$this->_stopToken = $token;
}
/**
* get current stop token
*
* @return string|false
*/
public function getStopToken()
{
return $this->_stopToken;
}
/** /**
* Render top tree * Render top tree
* *
* @param mixed $context current context * @param mixed $context current context
* @param string $waitFor return on this string
* *
* @return string * @return string
*/ */
public function render($context, $waitFor = false) public function render($context)
{ {
if (!$context instanceof Handlebars_Context) { if (!$context instanceof Handlebars_Context) {
$context = new Handlebars_Context($context); $context = new Handlebars_Context($context);
@ -110,16 +134,16 @@ class Handlebars_Template
$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($waitFor) if (is_string($this->_stopToken)
&& $current[Handlebars_Tokenizer::TYPE] == Handlebars_Tokenizer::T_ESCAPED && $current[Handlebars_Tokenizer::TYPE] == Handlebars_Tokenizer::T_ESCAPED
&& $current[Handlebars_Tokenizer::NAME] === $waitFor && $current[Handlebars_Tokenizer::NAME] === $this->_stopToken
) { ) {
//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;
array_push($this->_stack, $newStack); array_push($this->_stack, $newStack);
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();
@ -154,12 +178,11 @@ class Handlebars_Template
/** /**
* Discard top tree * Discard top tree
* *
* @param mixed $context current context * @param mixed $context current context
* @param string $waitFor return on this string
* *
* @return string * @return string
*/ */
public function discard($context, $waitFor = false) public function discard($context)
{ {
if (!$context instanceof Handlebars_Context) { if (!$context instanceof Handlebars_Context) {
$context = new Handlebars_Context($context); $context = new Handlebars_Context($context);
@ -171,17 +194,17 @@ class Handlebars_Template
$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($waitFor) if (is_string($this->_stopToken)
&& $current[Handlebars_Tokenizer::TYPE] == Handlebars_Tokenizer::T_ESCAPED && $current[Handlebars_Tokenizer::TYPE] == Handlebars_Tokenizer::T_ESCAPED
&& $current[Handlebars_Tokenizer::NAME] === $waitFor && $current[Handlebars_Tokenizer::NAME] === $this->_stopToken
) { ) {
//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;
array_push($this->_stack, $newStack); array_push($this->_stack, $newStack);
break; break;
} }
} }
return ''; return '';
} }