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
|
/**
|
24
|
* Parses a token and returns a node.
|
25
|
*
|
26
|
* @param Twig_Token $token A Twig_Token instance
|
27
|
*
|
28
|
* @return Twig_NodeInterface A Twig_NodeInterface instance
|
29
|
*/
|
30
|
public function parse(Twig_Token $token)
|
31
|
{
|
32
|
$lineno = $token->getLine();
|
33
|
$name = $this->parser->getStream()->expect(Twig_Token::NAME_TYPE)->getValue();
|
34
|
|
35
|
$arguments = $this->parser->getExpressionParser()->parseArguments();
|
36
|
|
37
|
$this->parser->getStream()->expect(Twig_Token::BLOCK_END_TYPE);
|
38
|
$this->parser->pushLocalScope();
|
39
|
$body = $this->parser->subparse(array($this, 'decideBlockEnd'), true);
|
40
|
if ($this->parser->getStream()->test(Twig_Token::NAME_TYPE)) {
|
41
|
$value = $this->parser->getStream()->next()->getValue();
|
42
|
|
43
|
if ($value != $name) {
|
44
|
throw new Twig_Error_Syntax(sprintf("Expected endmacro for macro '$name' (but %s given)", $value), $lineno);
|
45
|
}
|
46
|
}
|
47
|
$this->parser->popLocalScope();
|
48
|
$this->parser->getStream()->expect(Twig_Token::BLOCK_END_TYPE);
|
49
|
|
50
|
$this->parser->setMacro($name, new Twig_Node_Macro($name, new Twig_Node_Body(array($body)), $arguments, $lineno, $this->getTag()));
|
51
|
|
52
|
return null;
|
53
|
}
|
54
|
|
55
|
public function decideBlockEnd(Twig_Token $token)
|
56
|
{
|
57
|
return $token->test('endmacro');
|
58
|
}
|
59
|
|
60
|
/**
|
61
|
* Gets the tag name associated with this token parser.
|
62
|
*
|
63
|
* @return string The tag name
|
64
|
*/
|
65
|
public function getTag()
|
66
|
{
|
67
|
return 'macro';
|
68
|
}
|
69
|
}
|