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

nette / latte / 6806506485

09 Nov 2023 02:09AM UTC coverage: 94.014% (+0.007%) from 94.007%
6806506485

push

github

dg
Released version 3.0.11

4963 of 5279 relevant lines covered (94.01%)

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
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
declare(strict_types=1);
9

10
namespace Latte\Essential\Nodes;
11

12
use Latte\CompileException;
13
use Latte\Compiler\Nodes\AreaNode;
14
use Latte\Compiler\Nodes\AuxiliaryNode;
15
use Latte\Compiler\Nodes\Html\ElementNode;
16
use Latte\Compiler\Nodes\StatementNode;
17
use Latte\Compiler\PrintContext;
18
use Latte\Compiler\Tag;
19
use Latte\Compiler\TemplateParser;
20

21

22
/**
23
 * n:ifcontent
24
 */
25
class IfContentNode extends StatementNode
26
{
27
        public AreaNode $content;
28
        public int $id;
29
        public ElementNode $htmlElement;
30
        public ?AreaNode $else = null;
31

32

33
        /** @return \Generator<int, ?array, array{AreaNode, ?Tag}, static> */
34
        public static function create(Tag $tag, TemplateParser $parser): \Generator
1✔
35
        {
36
                $node = $tag->node = new static;
1✔
37
                $node->id = $parser->generateId();
1✔
38
                [$node->content] = yield;
1✔
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
                try {
50
                        $saved = $this->htmlElement->content;
1✔
51
                        $else = $this->else ?? new AuxiliaryNode(fn() => '');
1✔
52
                        $this->htmlElement->content = new AuxiliaryNode(fn() => <<<XX
1✔
53
                                ob_start();
1✔
54
                                try {
55
                                        {$saved->print($context)}
1✔
56
                                } finally {
57
                                        \$ʟ_ifc[$this->id] = rtrim(ob_get_flush()) === '';
1✔
58
                                }
59

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

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

80

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