• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

nette / latte / 22359082277

24 Feb 2026 04:03PM UTC coverage: 93.959% (+0.05%) from 93.907%
22359082277

push

github

dg
fixed operator ! priority

5273 of 5612 relevant lines covered (93.96%)

0.94 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.83
/src/Latte/Essential/Nodes/EmbedNode.php
1
<?php declare(strict_types=1);
2

3
/**
4
 * This file is part of the Latte (https://latte.nette.org)
5
 * Copyright (c) 2008 David Grudl (https://davidgrudl.com)
6
 */
7

8
namespace Latte\Essential\Nodes;
9

10
use Latte\CompileException;
11
use Latte\Compiler\Nodes\FragmentNode;
12
use Latte\Compiler\Nodes\Php\Expression\ArrayNode;
13
use Latte\Compiler\Nodes\Php\ExpressionNode;
14
use Latte\Compiler\Nodes\Php\Scalar\StringNode;
15
use Latte\Compiler\Nodes\StatementNode;
16
use Latte\Compiler\Nodes\TextNode;
17
use Latte\Compiler\PrintContext;
18
use Latte\Compiler\Tag;
19
use Latte\Compiler\TemplateParser;
20
use function count, preg_match;
21

22

23
/**
24
 * {embed [block|file] name [,] [params]}
25
 */
26
class EmbedNode extends StatementNode
27
{
28
        public ExpressionNode $name;
29
        public string $mode;
30
        public ArrayNode $args;
31
        public FragmentNode $blocks;
32
        public int|string|null $layer;
33

34

35
        /** @return \Generator<int, ?array, array{FragmentNode, ?Tag}, static> */
36
        public static function create(Tag $tag, TemplateParser $parser): \Generator
1✔
37
        {
38
                if ($tag->isNAttribute()) {
1✔
39
                        throw new CompileException('Attribute n:embed is not supported.', $tag->position);
×
40
                }
41

42
                $tag->outputMode = $tag::OutputRemoveIndentation;
1✔
43
                $tag->expectArguments();
1✔
44

45
                $node = $tag->node = new static;
1✔
46
                $mode = $tag->parser->tryConsumeTokenBeforeUnquotedString('block', 'file')?->text;
1✔
47
                $node->name = $tag->parser->parseUnquotedStringOrExpression();
1✔
48
                $node->mode = $mode ?? ($node->name instanceof StringNode && preg_match('~[\w-]+$~DA', $node->name->value) ? 'block' : 'file');
1✔
49
                $tag->parser->stream->tryConsume(',');
1✔
50
                $node->args = $tag->parser->parseArguments();
1✔
51

52
                $prevIndex = $parser->blockLayer;
1✔
53
                $parser->blockLayer = $node->layer = count($parser->blocks);
1✔
54
                $parser->blocks[$parser->blockLayer] = [];
1✔
55
                [$node->blocks] = yield;
1✔
56

57
                foreach ($node->blocks->children as $child) {
1✔
58
                        if (!$child instanceof ImportNode && !$child instanceof BlockNode && !$child instanceof TextNode) {
1✔
59
                                throw new CompileException('Unexpected content inside {embed} tags.', $child->position);
1✔
60
                        }
61
                }
62

63
                $parser->blockLayer = $prevIndex;
1✔
64
                return $node;
1✔
65
        }
66

67

68
        public function print(PrintContext $context): string
1✔
69
        {
70
                $imports = '';
1✔
71
                foreach ($this->blocks->children as $child) {
1✔
72
                        if ($child instanceof ImportNode) {
1✔
73
                                $imports .= $child->print($context);
1✔
74
                        } else {
75
                                $child->print($context);
1✔
76
                        }
77
                }
78

79
                return $this->mode === 'file'
1✔
80
                        ? $context->format(
1✔
81
                                <<<'XX'
82
                                        $this->enterBlockLayer(%dump, get_defined_vars()) %line; %raw
1✔
83
                                        try {
84
                                                $this->createTemplate(%raw, %node, "embed")->renderToContentType(%dump) %1.line;
85
                                        } finally {
86
                                                $this->leaveBlockLayer();
87
                                        }
88

89
                                        XX,
90
                                $this->layer,
1✔
91
                                $this->position,
1✔
92
                                $imports,
93
                                $context->ensureString($this->name, 'Template name'),
1✔
94
                                $this->args,
1✔
95
                                $context->getEscaper()->export(),
1✔
96
                        )
97
                        : $context->format(
1✔
98
                                <<<'XX'
99
                                        $this->enterBlockLayer(%dump, get_defined_vars()) %line; %raw
1✔
100
                                        $this->copyBlockLayer();
101
                                        try {
102
                                                $this->renderBlock(%raw, %node, %dump) %1.line;
103
                                        } finally {
104
                                                $this->leaveBlockLayer();
105
                                        }
106

107
                                        XX,
108
                                $this->layer,
1✔
109
                                $this->position,
1✔
110
                                $imports,
111
                                $context->ensureString($this->name, 'Block name'),
1✔
112
                                $this->args,
1✔
113
                                $context->getEscaper()->export(),
1✔
114
                        );
115
        }
116

117

118
        public function &getIterator(): \Generator
1✔
119
        {
120
                yield $this->name;
1✔
121
                yield $this->args;
1✔
122
                yield $this->blocks;
1✔
123
        }
1✔
124
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc