Skip to content

Commit

Permalink
[BUGFIX] Fix contentAs feature when used on Layout level
Browse files Browse the repository at this point in the history
`<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.
  • Loading branch information
smichaelsen committed Oct 25, 2019
1 parent 2d2d5db commit 68db27f
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 5 deletions.
3 changes: 3 additions & 0 deletions examples/Resources/Private/Layouts/ContentAs.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<f:layout name="ContentAs" />

<f:render section="Main" contentAs="content">Content from layout via contentAs</f:section>
5 changes: 5 additions & 0 deletions examples/Resources/Private/Singles/ContentFromLayout.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<f:layout name="ContentAs" />

<f:section name="Main">
{content}
</f:section>
26 changes: 26 additions & 0 deletions examples/example_layoutcontentas.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

/*
* EXAMPLE: MVC pattern used with TYPO3.Fluid
*
* This examples shows how TYPO3.Fluid is integrated
* in an MVC context, highlighting which parts may
* be replaced in order to adapt the engine to your
* favorite MVC framework.
*
* The alternative to this is single file rendering
* - see the other example for that.
*/

require __DIR__ . '/include/view_init.php';

// Assign Layout name as ViewVariable which we will pass to f:layout as name
$view->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);
12 changes: 7 additions & 5 deletions src/View/AbstractTemplateView.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
6 changes: 6 additions & 0 deletions tests/Functional/ExamplesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
[
Expand Down
1 change: 1 addition & 0 deletions tests/Unit/View/TemplatePathsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 68db27f

Please sign in to comment.