Skip to content

Commit 68db27f

Browse files
committed
[BUGFIX] Fix contentAs feature when used on Layout level
`<f:render section="MySection" contentAs="content">some content</f:section>` did not work when called from a Layout because the `contentAs` variable was not passed to the template section in that case.
1 parent 2d2d5db commit 68db27f

6 files changed

Lines changed: 48 additions & 5 deletions

File tree

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<f:layout name="ContentAs" />
2+
3+
<f:render section="Main" contentAs="content">Content from layout via contentAs</f:section>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<f:layout name="ContentAs" />
2+
3+
<f:section name="Main">
4+
{content}
5+
</f:section>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
/*
4+
* EXAMPLE: MVC pattern used with TYPO3.Fluid
5+
*
6+
* This examples shows how TYPO3.Fluid is integrated
7+
* in an MVC context, highlighting which parts may
8+
* be replaced in order to adapt the engine to your
9+
* favorite MVC framework.
10+
*
11+
* The alternative to this is single file rendering
12+
* - see the other example for that.
13+
*/
14+
15+
require __DIR__ . '/include/view_init.php';
16+
17+
// Assign Layout name as ViewVariable which we will pass to f:layout as name
18+
$view->assign('layout', 'Dynamic');
19+
20+
// Set the template path and filename we will render
21+
$view->getTemplatePaths()->setTemplatePathAndFilename(__DIR__ . '/Resources/Private/Singles/ContentFromLayout.html');
22+
23+
$output = $view->render();
24+
25+
// Output of Controller "Default" action "Default" using helper from view_init.php
26+
example_output($output);

src/View/AbstractTemplateView.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -217,15 +217,17 @@ function($parent, TemplatePaths $paths) use ($layoutName) {
217217
public function renderSection($sectionName, array $variables = [], $ignoreUnknown = false)
218218
{
219219
$renderingContext = $this->getCurrentRenderingContext();
220-
220+
$renderingTypeOnNextLevel = $this->getCurrentRenderingType();
221221
if ($this->getCurrentRenderingType() === self::RENDERING_LAYOUT) {
222222
// in case we render a layout right now, we will render a section inside a TEMPLATE.
223223
$renderingTypeOnNextLevel = self::RENDERING_TEMPLATE;
224-
} else {
225-
$renderingContext = clone $renderingContext;
226-
$renderingContext->setVariableProvider($renderingContext->getVariableProvider()->getScopeCopy($variables));
227-
$renderingTypeOnNextLevel = $this->getCurrentRenderingType();
224+
if (empty($variables)) {
225+
$variables = $renderingContext->getVariableProvider()->getAll();
226+
}
228227
}
228+
$variableProvider = $renderingContext->getVariableProvider()->getScopeCopy($variables);
229+
$renderingContext = clone $renderingContext;
230+
$renderingContext->setVariableProvider($variableProvider);
229231

230232
try {
231233
$parsedTemplate = $this->getCurrentParsedTemplate();

tests/Functional/ExamplesTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,12 @@ public function getExampleScriptTestValues()
240240
'Rendered via DynamicLayout, section "Main":',
241241
]
242242
],
243+
'example_layoutcontentas.php' => [
244+
'example_layoutcontentas.php',
245+
[
246+
'Content from layout via contentAs',
247+
]
248+
],
243249
'example_cachestatic.php' => [
244250
'example_cachestatic.php',
245251
[

tests/Unit/View/TemplatePathsTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ public function testResolveFilesInFolders()
220220
[['examples/Resources/Private/Layouts/', 'examples/Resources/Private/Templates/Default/'], 'html']
221221
);
222222
$expected = [
223+
'examples/Resources/Private/Layouts/ContentAs.html',
223224
'examples/Resources/Private/Layouts/Default.html',
224225
'examples/Resources/Private/Layouts/Dynamic.html',
225226
'examples/Resources/Private/Templates/Default/Default.html',

0 commit comments

Comments
 (0)