From ad109a891dc746f0a19d054695d185cf08793ed8 Mon Sep 17 00:00:00 2001 From: Victor Welling Date: Wed, 3 Oct 2018 16:46:01 +0200 Subject: [PATCH] Undo regression in embedding templates This fixes a regression that occured as a result of adding support for inheritance and blocks that caused presenters to be invoked multiple times. Test coverage was added in 697c47790fbf664c8ccc98a509eded022e19689a. --- src/Twig/Node/DisplayEndNode.php | 4 ++++ src/Twig/Node/DisplayStartNode.php | 4 ++++ src/Twig/NodeVisitor/ModelNodeVisitor.php | 7 +++++++ tests/Integration/Embedding/EmbeddingTest.php | 2 -- tests/Integration/Embedding/Templates/page.twig | 2 +- 5 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/Twig/Node/DisplayEndNode.php b/src/Twig/Node/DisplayEndNode.php index 92c2848..c9c81c3 100644 --- a/src/Twig/Node/DisplayEndNode.php +++ b/src/Twig/Node/DisplayEndNode.php @@ -39,6 +39,10 @@ public function __construct(ModuleNode $module) */ public function compile(Compiler $compiler) { + if ($this->module->hasAttribute('is_embedded')) { + return; + } + $extension = Extension::class; $templateName = $this->getTemplateName(); $view = View::class; diff --git a/src/Twig/Node/DisplayStartNode.php b/src/Twig/Node/DisplayStartNode.php index ae825fa..6c60c4a 100644 --- a/src/Twig/Node/DisplayStartNode.php +++ b/src/Twig/Node/DisplayStartNode.php @@ -43,6 +43,10 @@ public function __construct(ModuleNode $module, FindPresentationModelInterface $ */ public function compile(Compiler $compiler) { + if ($this->module->hasAttribute('is_embedded')) { + return; + } + $presentationModel = $this->findPresentationModel->for($this->getTemplateName()); $compiler diff --git a/src/Twig/NodeVisitor/ModelNodeVisitor.php b/src/Twig/NodeVisitor/ModelNodeVisitor.php index c3c2615..30e01e1 100644 --- a/src/Twig/NodeVisitor/ModelNodeVisitor.php +++ b/src/Twig/NodeVisitor/ModelNodeVisitor.php @@ -52,6 +52,13 @@ public function leaveNode(Node $node, Environment $environment): Node return $node; } + if ($node->hasAttribute('embedded_templates')) { + /** @var ModuleNode $embeddedTemplate */ + foreach ($node->getAttribute('embedded_templates') as $embeddedTemplate) { + $embeddedTemplate->setAttribute('is_embedded', true); + } + } + $node->setNode('display_start', new DisplayStartNode($node, $this)); $node->setNode('display_end', new DisplayEndNode($node)); diff --git a/tests/Integration/Embedding/EmbeddingTest.php b/tests/Integration/Embedding/EmbeddingTest.php index 415a513..21da955 100644 --- a/tests/Integration/Embedding/EmbeddingTest.php +++ b/tests/Integration/Embedding/EmbeddingTest.php @@ -66,8 +66,6 @@ public function testEmbeddedTemplatesShouldReceiveVariablesPassedAsArguments() */ public function testPresentersShouldOnlyBeCalledOnce() { - $this->markTestSkipped('Presenters should only be called once: currently fails, fix pending.'); - $this->pagePresenter ->expects($this->once()) ->method('present'); diff --git a/tests/Integration/Embedding/Templates/page.twig b/tests/Integration/Embedding/Templates/page.twig index 19854d5..51b2d62 100644 --- a/tests/Integration/Embedding/Templates/page.twig +++ b/tests/Integration/Embedding/Templates/page.twig @@ -1,7 +1,7 @@ {% model 'Shoot\\Shoot\\Tests\\Integration\\Embedding\\Models\\Page' %} {% extends 'base.twig' %} {% block body %} - {% embed 'layout.twig' with { main_class: 'main--overriden'} only %} + {% embed 'layout.twig' with { main_class: 'main--overriden'} %} {% block main %}

{{ title }}

{{ content }}