mirror of
https://github.com/Mibew/handlebars.php.git
synced 2025-05-02 10:16:41 +03:00
Merge pull request #25 from boukeversteegh/master
Added support for @index and @key
This commit is contained in:
commit
4ef98744ef
@ -35,6 +35,17 @@ class Handlebars_Context
|
|||||||
*/
|
*/
|
||||||
protected $stack = array();
|
protected $stack = array();
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var index stack for sections
|
||||||
|
*/
|
||||||
|
protected $index = array();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var key stack for objects
|
||||||
|
*/
|
||||||
|
protected $key = array();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Mustache rendering Context constructor.
|
* Mustache rendering Context constructor.
|
||||||
*
|
*
|
||||||
@ -59,6 +70,26 @@ class Handlebars_Context
|
|||||||
array_push($this->stack, $value);
|
array_push($this->stack, $value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Push an Index onto the index stack
|
||||||
|
*
|
||||||
|
* @param int $index Index of the current section item.
|
||||||
|
*/
|
||||||
|
public function pushIndex($index)
|
||||||
|
{
|
||||||
|
array_push($this->index, $index);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Push a Key onto the key stack
|
||||||
|
*
|
||||||
|
* @param string $key Key of the current object property.
|
||||||
|
*/
|
||||||
|
public function pushKey($key)
|
||||||
|
{
|
||||||
|
array_push($this->key, $key);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Pop the last Context frame from the stack.
|
* Pop the last Context frame from the stack.
|
||||||
*
|
*
|
||||||
@ -69,6 +100,27 @@ class Handlebars_Context
|
|||||||
return array_pop($this->stack);
|
return array_pop($this->stack);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Pop the last index from the stack.
|
||||||
|
*
|
||||||
|
* @return int Last index
|
||||||
|
*/
|
||||||
|
public function popIndex()
|
||||||
|
{
|
||||||
|
return array_pop($this->index);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Pop the last key from the stack.
|
||||||
|
*
|
||||||
|
* @return string Last key
|
||||||
|
*/
|
||||||
|
public function popKey()
|
||||||
|
{
|
||||||
|
return array_pop($this->key);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the last Context frame.
|
* Get the last Context frame.
|
||||||
*
|
*
|
||||||
@ -79,6 +131,26 @@ class Handlebars_Context
|
|||||||
return end($this->stack);
|
return end($this->stack);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the index of current section item.
|
||||||
|
*
|
||||||
|
* @return mixed Last index
|
||||||
|
*/
|
||||||
|
public function lastIndex()
|
||||||
|
{
|
||||||
|
return end($this->index);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the key of current object property.
|
||||||
|
*
|
||||||
|
* @return mixed Last key
|
||||||
|
*/
|
||||||
|
public function lastKey()
|
||||||
|
{
|
||||||
|
return end($this->key);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Change the current context to one of current context members
|
* Change the current context to one of current context members
|
||||||
*
|
*
|
||||||
|
@ -104,10 +104,12 @@ class Handlebars_Helpers
|
|||||||
$tmp = $context->get($args);
|
$tmp = $context->get($args);
|
||||||
$buffer = '';
|
$buffer = '';
|
||||||
if (is_array($tmp) || $tmp instanceof Traversable) {
|
if (is_array($tmp) || $tmp instanceof Traversable) {
|
||||||
foreach ($tmp as $var) {
|
foreach ($tmp as $key => $var) {
|
||||||
|
$context->pushKey($key);
|
||||||
$context->push($var);
|
$context->push($var);
|
||||||
$buffer .= $template->render($context);
|
$buffer .= $template->render($context);
|
||||||
$context->pop();
|
$context->pop();
|
||||||
|
$context->popKey();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return $buffer;
|
return $buffer;
|
||||||
|
@ -249,6 +249,7 @@ class Handlebars_Template
|
|||||||
$current[Handlebars_Tokenizer::ARGS], //Arguments
|
$current[Handlebars_Tokenizer::ARGS], //Arguments
|
||||||
$source
|
$source
|
||||||
);
|
);
|
||||||
|
|
||||||
$return = call_user_func_array($helpers->$sectionName, $params);
|
$return = call_user_func_array($helpers->$sectionName, $params);
|
||||||
if ($return instanceof Handlebars_String) {
|
if ($return instanceof Handlebars_String) {
|
||||||
return $this->handlebars->loadString($return)->render($context);
|
return $this->handlebars->loadString($return)->render($context);
|
||||||
@ -264,10 +265,12 @@ class Handlebars_Template
|
|||||||
}
|
}
|
||||||
$buffer = '';
|
$buffer = '';
|
||||||
if (is_array($sectionVar) || $sectionVar instanceof Traversable) {
|
if (is_array($sectionVar) || $sectionVar instanceof Traversable) {
|
||||||
foreach ($sectionVar as $d) {
|
foreach ($sectionVar as $index => $d) {
|
||||||
|
$context->pushIndex($index);
|
||||||
$context->push($d);
|
$context->push($d);
|
||||||
$buffer .= $this->render($context);
|
$buffer .= $this->render($context);
|
||||||
$context->pop();
|
$context->pop();
|
||||||
|
$context->popIndex();
|
||||||
}
|
}
|
||||||
} elseif (is_object($sectionVar)) {
|
} elseif (is_object($sectionVar)) {
|
||||||
//Act like with
|
//Act like with
|
||||||
@ -333,7 +336,14 @@ class Handlebars_Template
|
|||||||
*/
|
*/
|
||||||
private function _variables($context, $current, $escaped)
|
private function _variables($context, $current, $escaped)
|
||||||
{
|
{
|
||||||
$value = $context->get($current[Handlebars_Tokenizer::NAME]);
|
$name = $current[Handlebars_Tokenizer::NAME];
|
||||||
|
$value = $context->get($name);
|
||||||
|
if( $name == '@index' ) {
|
||||||
|
return $context->lastIndex();
|
||||||
|
}
|
||||||
|
if( $name == '@key' ) {
|
||||||
|
return $context->lastKey();
|
||||||
|
}
|
||||||
if ($escaped) {
|
if ($escaped) {
|
||||||
$args = $this->handlebars->getEscapeArgs();
|
$args = $this->handlebars->getEscapeArgs();
|
||||||
array_unshift($args, $value);
|
array_unshift($args, $value);
|
||||||
|
Loading…
Reference in New Issue
Block a user