mirror of
				https://github.com/Mibew/handlebars.php.git
				synced 2025-11-04 04:05:07 +03:00 
			
		
		
		
	Merge pull request #40 from Mibew/each_else
Add "else" block to "each" helper
This commit is contained in:
		
						commit
						8c686a924a
					
				@ -117,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) {
 | 
			
		||||
@ -127,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
 | 
			
		||||
     *
 | 
			
		||||
 | 
			
		||||
@ -163,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