diff options
author | Nicolas LÅ“uillet <nicolas.loeuillet@gmail.com> | 2013-08-02 22:40:51 +0200 |
---|---|---|
committer | Nicolas LÅ“uillet <nicolas.loeuillet@gmail.com> | 2013-08-02 22:40:51 +0200 |
commit | a4565e88edbc8e3bd092a475469769c86a4c350c (patch) | |
tree | a6a3c935b03a23ff87575c8c315cf8ba78fe68c2 /inc/Twig/TokenParser/If.php | |
parent | f6c9baab3efeec1d0efa151e276fc08d5b58f9e9 (diff) | |
download | wallabag-a4565e88edbc8e3bd092a475469769c86a4c350c.tar.gz wallabag-a4565e88edbc8e3bd092a475469769c86a4c350c.tar.zst wallabag-a4565e88edbc8e3bd092a475469769c86a4c350c.zip |
add Twig & refactor poche
Diffstat (limited to 'inc/Twig/TokenParser/If.php')
-rw-r--r-- | inc/Twig/TokenParser/If.php | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/inc/Twig/TokenParser/If.php b/inc/Twig/TokenParser/If.php new file mode 100644 index 00000000..3d7d1f51 --- /dev/null +++ b/inc/Twig/TokenParser/If.php | |||
@@ -0,0 +1,94 @@ | |||
1 | <?php | ||
2 | |||
3 | /* | ||
4 | * This file is part of Twig. | ||
5 | * | ||
6 | * (c) 2009 Fabien Potencier | ||
7 | * (c) 2009 Armin Ronacher | ||
8 | * | ||
9 | * For the full copyright and license information, please view the LICENSE | ||
10 | * file that was distributed with this source code. | ||
11 | */ | ||
12 | |||
13 | /** | ||
14 | * Tests a condition. | ||
15 | * | ||
16 | * <pre> | ||
17 | * {% if users %} | ||
18 | * <ul> | ||
19 | * {% for user in users %} | ||
20 | * <li>{{ user.username|e }}</li> | ||
21 | * {% endfor %} | ||
22 | * </ul> | ||
23 | * {% endif %} | ||
24 | * </pre> | ||
25 | */ | ||
26 | class Twig_TokenParser_If extends Twig_TokenParser | ||
27 | { | ||
28 | /** | ||
29 | * Parses a token and returns a node. | ||
30 | * | ||
31 | * @param Twig_Token $token A Twig_Token instance | ||
32 | * | ||
33 | * @return Twig_NodeInterface A Twig_NodeInterface instance | ||
34 | */ | ||
35 | public function parse(Twig_Token $token) | ||
36 | { | ||
37 | $lineno = $token->getLine(); | ||
38 | $expr = $this->parser->getExpressionParser()->parseExpression(); | ||
39 | $stream = $this->parser->getStream(); | ||
40 | $stream->expect(Twig_Token::BLOCK_END_TYPE); | ||
41 | $body = $this->parser->subparse(array($this, 'decideIfFork')); | ||
42 | $tests = array($expr, $body); | ||
43 | $else = null; | ||
44 | |||
45 | $end = false; | ||
46 | while (!$end) { | ||
47 | switch ($stream->next()->getValue()) { | ||
48 | case 'else': | ||
49 | $stream->expect(Twig_Token::BLOCK_END_TYPE); | ||
50 | $else = $this->parser->subparse(array($this, 'decideIfEnd')); | ||
51 | break; | ||
52 | |||
53 | case 'elseif': | ||
54 | $expr = $this->parser->getExpressionParser()->parseExpression(); | ||
55 | $stream->expect(Twig_Token::BLOCK_END_TYPE); | ||
56 | $body = $this->parser->subparse(array($this, 'decideIfFork')); | ||
57 | $tests[] = $expr; | ||
58 | $tests[] = $body; | ||
59 | break; | ||
60 | |||
61 | case 'endif': | ||
62 | $end = true; | ||
63 | break; | ||
64 | |||
65 | default: | ||
66 | throw new Twig_Error_Syntax(sprintf('Unexpected end of template. Twig was looking for the following tags "else", "elseif", or "endif" to close the "if" block started at line %d)', $lineno), $stream->getCurrent()->getLine(), $stream->getFilename()); | ||
67 | } | ||
68 | } | ||
69 | |||
70 | $stream->expect(Twig_Token::BLOCK_END_TYPE); | ||
71 | |||
72 | return new Twig_Node_If(new Twig_Node($tests), $else, $lineno, $this->getTag()); | ||
73 | } | ||
74 | |||
75 | public function decideIfFork(Twig_Token $token) | ||
76 | { | ||
77 | return $token->test(array('elseif', 'else', 'endif')); | ||
78 | } | ||
79 | |||
80 | public function decideIfEnd(Twig_Token $token) | ||
81 | { | ||
82 | return $token->test(array('endif')); | ||
83 | } | ||
84 | |||
85 | /** | ||
86 | * Gets the tag name associated with this token parser. | ||
87 | * | ||
88 | * @return string The tag name | ||
89 | */ | ||
90 | public function getTag() | ||
91 | { | ||
92 | return 'if'; | ||
93 | } | ||
94 | } | ||