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

nette / latte / 22368407632

24 Feb 2026 08:17PM UTC coverage: 94.825% (-0.2%) from 95.054%
22368407632

push

github

dg
phpstan

135 of 147 new or added lines in 38 files covered. (91.84%)

186 existing lines in 70 files now uncovered.

5534 of 5836 relevant lines covered (94.83%)

0.95 hits per line

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

96.0
/src/Latte/Essential/Nodes/IfContentNode.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\AreaNode;
12
use Latte\Compiler\Nodes\AuxiliaryNode;
13
use Latte\Compiler\Nodes\Html\ElementNode;
14
use Latte\Compiler\Nodes\StatementNode;
15
use Latte\Compiler\PrintContext;
16
use Latte\Compiler\Tag;
17
use Latte\Compiler\TemplateParser;
18

19

20
/**
21
 * <div n:ifcontent> ... </div>
22
 * Omits element if content is empty or whitespace only.
23
 */
24
class IfContentNode extends StatementNode
25
{
26
        public AreaNode $content;
27
        public int $id;
28
        public ElementNode $htmlElement;
29
        public ?AreaNode $else = null;
30

31

32
        /** @return \Generator<int, ?list<string>, array{AreaNode, ?Tag}, static> */
33
        public static function create(Tag $tag, TemplateParser $parser): \Generator
1✔
34
        {
35
                $node = $tag->node = new static;
1✔
36
                $node->id = $parser->generateId();
1✔
37
                [$node->content] = yield;
1✔
38
                assert($tag->htmlElement !== null);
39
                $node->htmlElement = $tag->htmlElement;
1✔
40
                if (!$node->htmlElement->content) {
1✔
41
                        throw new CompileException("Unnecessary n:ifcontent on empty element <{$node->htmlElement->name}>", $tag->position);
1✔
42
                }
43
                return $node;
1✔
44
        }
45

46

47
        public function print(PrintContext $context): string
1✔
48
        {
49
                $saved = $this->htmlElement->content;
1✔
50
                assert($saved !== null);
51
                try {
52
                        $else = $this->else ?? new AuxiliaryNode(fn() => '');
1✔
53
                        $this->htmlElement->content = new AuxiliaryNode(fn() => <<<XX
1✔
54
                                ob_start();
1✔
55
                                try {
56
                                        {$saved->print($context)}
1✔
57
                                } finally {
58
                                        \$ʟ_ifc[$this->id] = rtrim(ob_get_flush()) === '';
1✔
59
                                }
60

61
                                XX);
1✔
62
                        return <<<XX
63
                                ob_start(fn() => '');
1✔
64
                                try {
65
                                        {$this->content->print($context)}
1✔
66
                                } finally {
67
                                        if (\$ʟ_ifc[$this->id] ?? null) {
1✔
68
                                                ob_end_clean();
69
                                                {$else->print($context)}
1✔
70
                                        } else {
71
                                                echo ob_get_clean();
72
                                        }
73
                                }
74

75
                                XX;
UNCOV
76
                } finally {
×
77
                        $this->htmlElement->content = $saved;
1✔
78
                }
79
        }
80

81

82
        public function &getIterator(): \Generator
1✔
83
        {
84
                yield $this->content;
1✔
85
        }
1✔
86
}
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