From 68db27f83e55fcd4bf1c986f8a52b2fb86768746 Mon Sep 17 00:00:00 2001 From: Sebastian Michaelsen Date: Wed, 31 Jul 2019 14:56:25 +0200 Subject: [PATCH] [BUGFIX] Fix contentAs feature when used on Layout level `some content` did not work when called from a Layout because the `contentAs` variable was not passed to the template section in that case. --- .../Resources/Private/Layouts/ContentAs.html | 3 +++ .../Private/Singles/ContentFromLayout.html | 5 ++++ examples/example_layoutcontentas.php | 26 +++++++++++++++++++ src/View/AbstractTemplateView.php | 12 +++++---- tests/Functional/ExamplesTest.php | 6 +++++ tests/Unit/View/TemplatePathsTest.php | 1 + 6 files changed, 48 insertions(+), 5 deletions(-) create mode 100644 examples/Resources/Private/Layouts/ContentAs.html create mode 100644 examples/Resources/Private/Singles/ContentFromLayout.html create mode 100644 examples/example_layoutcontentas.php diff --git a/examples/Resources/Private/Layouts/ContentAs.html b/examples/Resources/Private/Layouts/ContentAs.html new file mode 100644 index 000000000..47b354af1 --- /dev/null +++ b/examples/Resources/Private/Layouts/ContentAs.html @@ -0,0 +1,3 @@ + + +Content from layout via contentAs diff --git a/examples/Resources/Private/Singles/ContentFromLayout.html b/examples/Resources/Private/Singles/ContentFromLayout.html new file mode 100644 index 000000000..89e6ac320 --- /dev/null +++ b/examples/Resources/Private/Singles/ContentFromLayout.html @@ -0,0 +1,5 @@ + + + +{content} + diff --git a/examples/example_layoutcontentas.php b/examples/example_layoutcontentas.php new file mode 100644 index 000000000..9d048702a --- /dev/null +++ b/examples/example_layoutcontentas.php @@ -0,0 +1,26 @@ +assign('layout', 'Dynamic'); + +// Set the template path and filename we will render +$view->getTemplatePaths()->setTemplatePathAndFilename(__DIR__ . '/Resources/Private/Singles/ContentFromLayout.html'); + +$output = $view->render(); + +// Output of Controller "Default" action "Default" using helper from view_init.php +example_output($output); diff --git a/src/View/AbstractTemplateView.php b/src/View/AbstractTemplateView.php index 9e2d5ba4d..35a286588 100644 --- a/src/View/AbstractTemplateView.php +++ b/src/View/AbstractTemplateView.php @@ -217,15 +217,17 @@ function($parent, TemplatePaths $paths) use ($layoutName) { public function renderSection($sectionName, array $variables = [], $ignoreUnknown = false) { $renderingContext = $this->getCurrentRenderingContext(); - + $renderingTypeOnNextLevel = $this->getCurrentRenderingType(); if ($this->getCurrentRenderingType() === self::RENDERING_LAYOUT) { // in case we render a layout right now, we will render a section inside a TEMPLATE. $renderingTypeOnNextLevel = self::RENDERING_TEMPLATE; - } else { - $renderingContext = clone $renderingContext; - $renderingContext->setVariableProvider($renderingContext->getVariableProvider()->getScopeCopy($variables)); - $renderingTypeOnNextLevel = $this->getCurrentRenderingType(); + if (empty($variables)) { + $variables = $renderingContext->getVariableProvider()->getAll(); + } } + $variableProvider = $renderingContext->getVariableProvider()->getScopeCopy($variables); + $renderingContext = clone $renderingContext; + $renderingContext->setVariableProvider($variableProvider); try { $parsedTemplate = $this->getCurrentParsedTemplate(); diff --git a/tests/Functional/ExamplesTest.php b/tests/Functional/ExamplesTest.php index 4a1682f36..8f635fc96 100644 --- a/tests/Functional/ExamplesTest.php +++ b/tests/Functional/ExamplesTest.php @@ -240,6 +240,12 @@ public function getExampleScriptTestValues() 'Rendered via DynamicLayout, section "Main":', ] ], + 'example_layoutcontentas.php' => [ + 'example_layoutcontentas.php', + [ + 'Content from layout via contentAs', + ] + ], 'example_cachestatic.php' => [ 'example_cachestatic.php', [ diff --git a/tests/Unit/View/TemplatePathsTest.php b/tests/Unit/View/TemplatePathsTest.php index bc260e460..6db9940d4 100644 --- a/tests/Unit/View/TemplatePathsTest.php +++ b/tests/Unit/View/TemplatePathsTest.php @@ -220,6 +220,7 @@ public function testResolveFilesInFolders() [['examples/Resources/Private/Layouts/', 'examples/Resources/Private/Templates/Default/'], 'html'] ); $expected = [ + 'examples/Resources/Private/Layouts/ContentAs.html', 'examples/Resources/Private/Layouts/Default.html', 'examples/Resources/Private/Layouts/Dynamic.html', 'examples/Resources/Private/Templates/Default/Default.html',