mirror of
https://github.com/Mibew/handlebars.php.git
synced 2024-11-15 08:44:12 +03:00
Add "else" block to "each" helper
This commit is contained in:
parent
c83e0fbbf7
commit
93884fb26b
11
src/Handlebars/Helpers.php
Executable file → Normal file
11
src/Handlebars/Helpers.php
Executable file → Normal file
@ -9,6 +9,7 @@
|
||||
* @package Handlebars
|
||||
* @author fzerorubigd <fzerorubigd@gmail.com>
|
||||
* @author Behrooz Shabani <everplays@gmail.com>
|
||||
* @author Dmitriy Simushev <simushevds@gmail.com>
|
||||
* @copyright 2012 (c) ParsPooyesh Co
|
||||
* @copyright 2013 (c) Behrooz Shabani
|
||||
* @license MIT <http://opensource.org/licenses/MIT>
|
||||
@ -116,7 +117,13 @@ class Helpers
|
||||
*/
|
||||
$tmp = $context->get($args);
|
||||
$buffer = '';
|
||||
if (is_array($tmp) || $tmp instanceof \Traversable) {
|
||||
|
||||
if (!$tmp) {
|
||||
$template->setStopToken('else');
|
||||
$template->discard();
|
||||
$template->setStopToken(false);
|
||||
$buffer = $template->render($context);
|
||||
} elseif (is_array($tmp) || $tmp instanceof \Traversable) {
|
||||
$islist = ($tmp instanceof \Generator) || (array_keys($tmp) == range(0, count($tmp) - 1));
|
||||
|
||||
foreach ($tmp as $key => $var) {
|
||||
@ -126,6 +133,8 @@ class Helpers
|
||||
$context->pushKey($key);
|
||||
}
|
||||
$context->push($var);
|
||||
$template->setStopToken('else');
|
||||
$template->rewind();
|
||||
$buffer .= $template->render($context);
|
||||
$context->pop();
|
||||
if ($islist) {
|
||||
|
13
src/Handlebars/Template.php
Executable file → Normal file
13
src/Handlebars/Template.php
Executable file → Normal file
@ -10,6 +10,7 @@
|
||||
* @author fzerorubigd <fzerorubigd@gmail.com>
|
||||
* @author Behrooz Shabani <everplays@gmail.com>
|
||||
* @author Chris Gray <chris.w.gray@gmail.com>
|
||||
* @author Dmitriy Simushev <simushevds@gmail.com>
|
||||
* @copyright 2012 (c) ParsPooyesh Co
|
||||
* @copyright 2013 (c) Behrooz Shabani
|
||||
* @license MIT <http://opensource.org/licenses/MIT>
|
||||
@ -230,6 +231,18 @@ class Template
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Rewind top tree index to the first element
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function rewind()
|
||||
{
|
||||
$topStack = array_pop($this->_stack);
|
||||
$topStack[0] = 0;
|
||||
array_push($this->_stack, $topStack);
|
||||
}
|
||||
|
||||
/**
|
||||
* Process section nodes
|
||||
*
|
||||
|
@ -8,6 +8,7 @@
|
||||
* @category Xamin
|
||||
* @package Handlebars
|
||||
* @author fzerorubigd <fzerorubigd@gmail.com>
|
||||
* @author Dmitriy Simushev <simushevds@gmail.com>
|
||||
* @copyright 2013 (c) f0ruD A
|
||||
* @license MIT <http://opensource.org/licenses/MIT>
|
||||
* @version GIT: $Id$
|
||||
@ -162,6 +163,16 @@ class HandlebarsTest extends \PHPUnit_Framework_TestCase
|
||||
array('data' => array('key1'=>1, 'key2'=>2,)),
|
||||
'key1=>1key2=>2'
|
||||
),
|
||||
array(
|
||||
'{{#each data}}{{this}}{{else}}fail{{/each}}',
|
||||
array('data' => array(1, 2, 3, 4)),
|
||||
'1234'
|
||||
),
|
||||
array(
|
||||
'{{#each data}}fail{{else}}ok{{/each}}',
|
||||
array('data' => false),
|
||||
'ok'
|
||||
),
|
||||
array(
|
||||
'{{#unless data}}ok{{/unless}}',
|
||||
array('data' => true),
|
||||
|
Loading…
Reference in New Issue
Block a user