• 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

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
 * n:ifcontent
22
 */
23
class IfContentNode extends StatementNode
24
{
25
        public AreaNode $content;
26
        public int $id;
27
        public ElementNode $htmlElement;
28
        public ?AreaNode $else = null;
29

30

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

44

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

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

72
                                XX;
73
                } finally {
×
74
                        $this->htmlElement->content = $saved;
1✔
75
                }
76
        }
77

78

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