mirror of
https://github.com/Mibew/handlebars.php.git
synced 2025-05-03 02:26:41 +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
|
* @package Handlebars
|
||||||
* @author fzerorubigd <fzerorubigd@gmail.com>
|
* @author fzerorubigd <fzerorubigd@gmail.com>
|
||||||
* @author Behrooz Shabani <everplays@gmail.com>
|
* @author Behrooz Shabani <everplays@gmail.com>
|
||||||
|
* @author Dmitriy Simushev <simushevds@gmail.com>
|
||||||
* @copyright 2012 (c) ParsPooyesh Co
|
* @copyright 2012 (c) ParsPooyesh Co
|
||||||
* @copyright 2013 (c) Behrooz Shabani
|
* @copyright 2013 (c) Behrooz Shabani
|
||||||
* @license MIT <http://opensource.org/licenses/MIT>
|
* @license MIT <http://opensource.org/licenses/MIT>
|
||||||
@ -116,7 +117,13 @@ class Helpers
|
|||||||
*/
|
*/
|
||||||
$tmp = $context->get($args);
|
$tmp = $context->get($args);
|
||||||
$buffer = '';
|
$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));
|
$islist = ($tmp instanceof \Generator) || (array_keys($tmp) == range(0, count($tmp) - 1));
|
||||||
|
|
||||||
foreach ($tmp as $key => $var) {
|
foreach ($tmp as $key => $var) {
|
||||||
@ -126,6 +133,8 @@ class Helpers
|
|||||||
$context->pushKey($key);
|
$context->pushKey($key);
|
||||||
}
|
}
|
||||||
$context->push($var);
|
$context->push($var);
|
||||||
|
$template->setStopToken('else');
|
||||||
|
$template->rewind();
|
||||||
$buffer .= $template->render($context);
|
$buffer .= $template->render($context);
|
||||||
$context->pop();
|
$context->pop();
|
||||||
if ($islist) {
|
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 fzerorubigd <fzerorubigd@gmail.com>
|
||||||
* @author Behrooz Shabani <everplays@gmail.com>
|
* @author Behrooz Shabani <everplays@gmail.com>
|
||||||
* @author Chris Gray <chris.w.gray@gmail.com>
|
* @author Chris Gray <chris.w.gray@gmail.com>
|
||||||
|
* @author Dmitriy Simushev <simushevds@gmail.com>
|
||||||
* @copyright 2012 (c) ParsPooyesh Co
|
* @copyright 2012 (c) ParsPooyesh Co
|
||||||
* @copyright 2013 (c) Behrooz Shabani
|
* @copyright 2013 (c) Behrooz Shabani
|
||||||
* @license MIT <http://opensource.org/licenses/MIT>
|
* @license MIT <http://opensource.org/licenses/MIT>
|
||||||
@ -230,6 +231,18 @@ class Template
|
|||||||
return '';
|
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
|
* Process section nodes
|
||||||
*
|
*
|
||||||
|
@ -8,6 +8,7 @@
|
|||||||
* @category Xamin
|
* @category Xamin
|
||||||
* @package Handlebars
|
* @package Handlebars
|
||||||
* @author fzerorubigd <fzerorubigd@gmail.com>
|
* @author fzerorubigd <fzerorubigd@gmail.com>
|
||||||
|
* @author Dmitriy Simushev <simushevds@gmail.com>
|
||||||
* @copyright 2013 (c) f0ruD A
|
* @copyright 2013 (c) f0ruD A
|
||||||
* @license MIT <http://opensource.org/licenses/MIT>
|
* @license MIT <http://opensource.org/licenses/MIT>
|
||||||
* @version GIT: $Id$
|
* @version GIT: $Id$
|
||||||
@ -162,6 +163,16 @@ class HandlebarsTest extends \PHPUnit_Framework_TestCase
|
|||||||
array('data' => array('key1'=>1, 'key2'=>2,)),
|
array('data' => array('key1'=>1, 'key2'=>2,)),
|
||||||
'key1=>1key2=>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(
|
array(
|
||||||
'{{#unless data}}ok{{/unless}}',
|
'{{#unless data}}ok{{/unless}}',
|
||||||
array('data' => true),
|
array('data' => true),
|
||||||
|
Loading…
Reference in New Issue
Block a user