shop.balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

Macro.php (1778B)


      1 <?php
      2 
      3 /*
      4  * This file is part of Twig.
      5  *
      6  * (c) 2009 Fabien Potencier
      7  *
      8  * For the full copyright and license information, please view the LICENSE
      9  * file that was distributed with this source code.
     10  */
     11 
     12 /**
     13  * Defines a macro.
     14  *
     15  * <pre>
     16  * {% macro input(name, value, type, size) %}
     17  *    <input type="{{ type|default('text') }}" name="{{ name }}" value="{{ value|e }}" size="{{ size|default(20) }}" />
     18  * {% endmacro %}
     19  * </pre>
     20  */
     21 class Twig_TokenParser_Macro extends Twig_TokenParser
     22 {
     23     public function parse(Twig_Token $token)
     24     {
     25         $lineno = $token->getLine();
     26         $stream = $this->parser->getStream();
     27         $name = $stream->expect(Twig_Token::NAME_TYPE)->getValue();
     28 
     29         $arguments = $this->parser->getExpressionParser()->parseArguments(true, true);
     30 
     31         $stream->expect(Twig_Token::BLOCK_END_TYPE);
     32         $this->parser->pushLocalScope();
     33         $body = $this->parser->subparse(array($this, 'decideBlockEnd'), true);
     34         if ($token = $stream->nextIf(Twig_Token::NAME_TYPE)) {
     35             $value = $token->getValue();
     36 
     37             if ($value != $name) {
     38                 throw new Twig_Error_Syntax(sprintf('Expected endmacro for macro "%s" (but "%s" given).', $name, $value), $stream->getCurrent()->getLine(), $stream->getFilename());
     39             }
     40         }
     41         $this->parser->popLocalScope();
     42         $stream->expect(Twig_Token::BLOCK_END_TYPE);
     43 
     44         $this->parser->setMacro($name, new Twig_Node_Macro($name, new Twig_Node_Body(array($body)), $arguments, $lineno, $this->getTag()));
     45     }
     46 
     47     public function decideBlockEnd(Twig_Token $token)
     48     {
     49         return $token->test('endmacro');
     50     }
     51 
     52     public function getTag()
     53     {
     54         return 'macro';
     55     }
     56 }