blob: 28beb8ae477ba5136976be3a2084625c95801a2d [file] [log] [blame]
Matthias Andreas Benkard12a57352021-12-28 18:02:04 +01001<?php
2
3/*
4 * This file is part of Twig.
5 *
6 * (c) Fabien Potencier
7 * (c) 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
13namespace Twig\TokenParser;
14
15use Twig\Node\IncludeNode;
16use Twig\Node\Node;
17use Twig\Token;
18
19/**
20 * Includes a template.
21 *
22 * {% include 'header.html' %}
23 * Body
24 * {% include 'footer.html' %}
25 *
26 * @internal
27 */
28class IncludeTokenParser extends AbstractTokenParser
29{
30 public function parse(Token $token): Node
31 {
32 $expr = $this->parser->getExpressionParser()->parseExpression();
33
34 list($variables, $only, $ignoreMissing) = $this->parseArguments();
35
36 return new IncludeNode($expr, $variables, $only, $ignoreMissing, $token->getLine(), $this->getTag());
37 }
38
39 protected function parseArguments()
40 {
41 $stream = $this->parser->getStream();
42
43 $ignoreMissing = false;
44 if ($stream->nextIf(/* Token::NAME_TYPE */ 5, 'ignore')) {
45 $stream->expect(/* Token::NAME_TYPE */ 5, 'missing');
46
47 $ignoreMissing = true;
48 }
49
50 $variables = null;
51 if ($stream->nextIf(/* Token::NAME_TYPE */ 5, 'with')) {
52 $variables = $this->parser->getExpressionParser()->parseExpression();
53 }
54
55 $only = false;
56 if ($stream->nextIf(/* Token::NAME_TYPE */ 5, 'only')) {
57 $only = true;
58 }
59
60 $stream->expect(/* Token::BLOCK_END_TYPE */ 3);
61
62 return [$variables, $only, $ignoreMissing];
63 }
64
65 public function getTag(): string
66 {
67 return 'include';
68 }
69}