mirror of
https://github.com/Mibew/handlebars.php.git
synced 2025-05-02 18:26:40 +03:00
Add {{else}} support to if
This commit is contained in:
parent
600215d648
commit
ec6761fde4
@ -70,14 +70,13 @@ class Handlebars_Helpers
|
|||||||
'if',
|
'if',
|
||||||
function ($template, $context, $args, $source) {
|
function ($template, $context, $args, $source) {
|
||||||
$tmp = $context->get($args);
|
$tmp = $context->get($args);
|
||||||
$parts = explode('{{else}}', $source, 2);
|
|
||||||
$buffer = '';
|
$buffer = '';
|
||||||
if ($tmp) {
|
if ($tmp) {
|
||||||
$buffer = $template->getEngine()->render($parts[0], $context);
|
$buffer = $template->render($context, 'else');
|
||||||
|
$template->discard($context);
|
||||||
} else {
|
} else {
|
||||||
if (isset($parts[1])) {
|
$template->discard($context, 'else');
|
||||||
$buffer = $template->getEngine()->render($parts[1], $context);
|
$buffer = $template->render($context);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return $buffer;
|
return $buffer;
|
||||||
}
|
}
|
||||||
|
@ -93,10 +93,11 @@ class Handlebars_Template
|
|||||||
* 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)
|
public function render($context, $waitFor = false)
|
||||||
{
|
{
|
||||||
if (!$context instanceof Handlebars_Context) {
|
if (!$context instanceof Handlebars_Context) {
|
||||||
$context = new Handlebars_Context($context);
|
$context = new Handlebars_Context($context);
|
||||||
@ -108,6 +109,17 @@ class Handlebars_Template
|
|||||||
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 (is_string($waitFor)
|
||||||
|
&& $current[Handlebars_Tokenizer::TYPE] == Handlebars_Tokenizer::T_ESCAPED
|
||||||
|
&& $current[Handlebars_Tokenizer::NAME] === $waitFor
|
||||||
|
) {
|
||||||
|
//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]) {
|
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();
|
||||||
@ -139,6 +151,40 @@ class Handlebars_Template
|
|||||||
return $buffer;
|
return $buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Discard top tree
|
||||||
|
*
|
||||||
|
* @param mixed $context current context
|
||||||
|
* @param string $waitFor return on this string
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function discard($context, $waitFor = false)
|
||||||
|
{
|
||||||
|
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;
|
||||||
|
|
||||||
|
while (array_key_exists($index, $tree)) {
|
||||||
|
$current = $tree[$index];
|
||||||
|
$index++;
|
||||||
|
//if the section is exactly like waitFor
|
||||||
|
if (is_string($waitFor)
|
||||||
|
&& $current[Handlebars_Tokenizer::TYPE] == Handlebars_Tokenizer::T_ESCAPED
|
||||||
|
&& $current[Handlebars_Tokenizer::NAME] === $waitFor
|
||||||
|
) {
|
||||||
|
//Ok break here, the helper should be aware of this.
|
||||||
|
$newStack = array_pop($this->_stack);
|
||||||
|
$newStack[0] = $index;
|
||||||
|
array_push($this->_stack, $newStack);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Process section nodes
|
* Process section nodes
|
||||||
*
|
*
|
||||||
|
Loading…
Reference in New Issue
Block a user