From e85d3154db4bd4b74b1ee16532a6ac8d5993b2f2 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Wed, 1 Nov 2023 22:56:33 +0100 Subject: [PATCH] snippet wip --- src/Bridges/ApplicationLatte/Nodes/ControlNode.php | 8 ++++++-- src/Bridges/ApplicationLatte/SnippetRuntime.php | 3 +-- tests/Bridges.Latte3/renderSnippets.phpt | 2 ++ tests/Bridges.Latte3/{control}.phpt | 4 ++-- 4 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/Bridges/ApplicationLatte/Nodes/ControlNode.php b/src/Bridges/ApplicationLatte/Nodes/ControlNode.php index 2a28c4640..291d1be05 100644 --- a/src/Bridges/ApplicationLatte/Nodes/ControlNode.php +++ b/src/Bridges/ApplicationLatte/Nodes/ControlNode.php @@ -31,6 +31,7 @@ class ControlNode extends StatementNode public ?ExpressionNode $method = null; public ArrayNode $args; public ?bool $escape = null; + public bool $inSnippetArea = false; public static function create(Tag $tag): static @@ -40,6 +41,7 @@ public static function create(Tag $tag): static $stream = $tag->parser->stream; $node = new static; $node->name = $tag->parser->parseUnquotedStringOrExpression(colon: false); + $node->inSnippetArea = $tag->closestTag(['snippetArea']) && !$tag->closestTag(['snippet']); if ($stream->tryConsume(':')) { $node->method = $tag->parser->parseExpression(); } @@ -94,11 +96,14 @@ public function print(PrintContext $context): string $this->name, ); + $fetchCode .= $this->inSnippetArea + ? 'if ($ʟ_tmp instanceof Nette\Application\UI\Renderable) $ʟ_tmp->snippetMode = $this->global->uiControl->snippetMode;' + : 'if ($ʟ_tmp instanceof Nette\Application\UI\Renderable) $ʟ_tmp->redrawControl(null, $ʟ_tmp->snippetMode = false);'; + if ($this->escape) { return $context->format( <<<'XX' %raw - if ($ʟ_tmp instanceof Nette\Application\UI\Renderable) $ʟ_tmp->redrawControl(null, false); ob_start(fn() => ''); $ʟ_tmp->%raw(%args) %line; $ʟ_fi = new LR\FilterInfo(%dump); echo %modifyContent(ob_get_clean()); @@ -117,7 +122,6 @@ public function print(PrintContext $context): string return $context->format( <<<'XX' %raw - if ($ʟ_tmp instanceof Nette\Application\UI\Renderable) $ʟ_tmp->redrawControl(null, false); $ʟ_tmp->%raw(%args) %line; diff --git a/src/Bridges/ApplicationLatte/SnippetRuntime.php b/src/Bridges/ApplicationLatte/SnippetRuntime.php index cfd2d9af1..a859a8e3e 100644 --- a/src/Bridges/ApplicationLatte/SnippetRuntime.php +++ b/src/Bridges/ApplicationLatte/SnippetRuntime.php @@ -107,7 +107,6 @@ public function renderSnippets(array $blocks, array $params): bool } $this->renderingSnippets = true; - $this->control->snippetMode = false; foreach ($blocks as $name => $block) { if (!$this->control->isControlInvalid($name)) { continue; @@ -117,7 +116,7 @@ public function renderSnippets(array $blocks, array $params): bool $function($params); } - $this->control->snippetMode = true; + $this->control->redrawControl(null, false); $this->renderChildren(); return true; } diff --git a/tests/Bridges.Latte3/renderSnippets.phpt b/tests/Bridges.Latte3/renderSnippets.phpt index 95ab0ab6f..d8d04c2de 100644 --- a/tests/Bridges.Latte3/renderSnippets.phpt +++ b/tests/Bridges.Latte3/renderSnippets.phpt @@ -22,6 +22,7 @@ class InnerControl extends Nette\Application\UI\Control public function render() { $latte = new Latte\Engine; + $latte->setTempDirectory('temp'); $latte->addExtension(new Nette\Bridges\ApplicationLatte\UIExtension($this)); $params['say'] = 'Hello'; $latte->render(__DIR__ . '/templates/snippet-included.latte', $params); @@ -43,6 +44,7 @@ class TestPresenter extends Nette\Application\UI\Presenter public function render() { $latte = new Latte\Engine; + $latte->setTempDirectory('temp'); $latte->addExtension(new Nette\Bridges\ApplicationLatte\UIExtension($this)); $latte->render(__DIR__ . '/templates/snippet-include.latte'); } diff --git a/tests/Bridges.Latte3/{control}.phpt b/tests/Bridges.Latte3/{control}.phpt index 0b22e87d8..d7a424dba 100644 --- a/tests/Bridges.Latte3/{control}.phpt +++ b/tests/Bridges.Latte3/{control}.phpt @@ -25,7 +25,7 @@ Assert::match( <<<'XX' %A% $ʟ_tmp = $this->global->uiControl->getComponent('form'); - if ($ʟ_tmp instanceof Nette\Application\UI\Renderable) $ʟ_tmp->redrawControl(null, false); + if ($ʟ_tmp instanceof Nette\Application\UI\Renderable) $ʟ_tmp->redrawControl(null, $ʟ_tmp->snippetMode = false); $ʟ_tmp->render() /* line 1 */; %A% XX, @@ -36,7 +36,7 @@ Assert::match( <<<'XX' %A% if (!is_object($ʟ_tmp = $form)) $ʟ_tmp = $this->global->uiControl->getComponent($ʟ_tmp); - if ($ʟ_tmp instanceof Nette\Application\UI\Renderable) $ʟ_tmp->redrawControl(null, false); + if ($ʟ_tmp instanceof Nette\Application\UI\Renderable) $ʟ_tmp->redrawControl(null, $ʟ_tmp->snippetMode = false); $ʟ_tmp->render() /* line 1 */; %A% XX,