Skip to content

Commit

Permalink
Fix extending multiple levels of Contao templates (see #3203)
Browse files Browse the repository at this point in the history
Description
-----------

Fixes the `{% extends '@Contao/ce_text' %}` part of #3200

Commits
-------

aaec0be Fix extending multiple levels of Contao templates
c1a3c70 Add nested templates test
48daedc Fix coverage
  • Loading branch information
ausi committed Jul 28, 2021
1 parent e7244ea commit f596595
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 2 deletions.
8 changes: 6 additions & 2 deletions core-bundle/src/Twig/Extension/ContaoExtension.php
Expand Up @@ -173,8 +173,12 @@ public function renderLegacyTemplate(string $name, array $blocks, array $context
$partialTemplate = new class($template) extends FrontendTemplate {
public function setBlocks(array $blocks): void
{
$this->arrBlocks = $blocks;
$this->arrBlockNames = array_keys($blocks);
$this->arrBlocks = array_map(
static function ($block) {
return \is_array($block) ? $block : [$block];
},
$blocks
);
}

public function parse(): string
Expand Down
12 changes: 12 additions & 0 deletions core-bundle/src/Twig/Loader/ContaoFilesystemLoader.php
Expand Up @@ -213,6 +213,18 @@ public function getSourceContext($name): Source
return $source;
}

// Look up the blocks of the parent template if present
if (
1 === preg_match(
'/\$this\s*->\s*extend\s*\(\s*[\'"]([a-z0-9_-]+)[\'"]\s*\)/i',
file_get_contents($source->getPath()),
$match
)
&& '@Contao/'.$match[1].'.html5' !== $name
) {
return new Source($this->getSourceContext('@Contao/'.$match[1].'.html5')->getCode(), $source->getName(), $source->getPath());
}

preg_match_all(
'/\$this\s*->\s*block\s*\(\s*[\'"]([a-z0-9_-]+)[\'"]\s*\)/i',
file_get_contents($source->getPath()),
Expand Down
9 changes: 9 additions & 0 deletions core-bundle/tests/Fixtures/Twig/legacy/templates/bar.html5
@@ -0,0 +1,9 @@
...ignored
<?php echo $this->extend('foo'); ?>
...ignored
<?php $this->block('A'); ?>
bar before A
<?php $this->parent() ?>
bar after A
<?php $this->endblock(); ?>
...ignored
15 changes: 15 additions & 0 deletions core-bundle/tests/Fixtures/Twig/legacy/templates/baz.html5
@@ -0,0 +1,15 @@
...ignored
<?php echo $this->extend('bar'); ?>
...ignored
<?php $this->block('A'); ?>
baz before A
<?php $this->parent() ?>
baz after A
<?php $this->endblock(); ?>
...ignored
<?php $this->block('B'); ?>
baz before B
<?php $this->parent() ?>
baz after B
<?php $this->endblock(); ?>
...ignored
32 changes: 32 additions & 0 deletions core-bundle/tests/Twig/Extension/ContaoExtensionTest.php
Expand Up @@ -201,6 +201,38 @@ public function testRenderLegacyTemplate(): void
$this->assertSame("foo: bar\noriginal A block\noverwritten B block", $output);
}

public function testRenderLegacyTemplateNested(): void
{
$extension = $this->getContaoExtension();

System::setContainer($this->getContainerWithContaoConfiguration(
Path::canonicalize(__DIR__.'/../../Fixtures/Twig/legacy')
));

$output = $extension->renderLegacyTemplate(
'baz.html5',
['B' => "root before B\n[[TL_PARENT]]root after B"],
['foo' => 'bar']
);

$this->assertSame(
implode("\n", [
'foo: bar',
'baz before A',
'bar before A',
'original A block',
'bar after A',
'baz after A',
'root before B',
'baz before B',
'original B block',
'baz after B',
'root after B',
]),
$output
);
}

/**
* @param Environment&MockObject $environment
*/
Expand Down
16 changes: 16 additions & 0 deletions core-bundle/tests/Twig/Loader/ContaoFilesystemLoaderTest.php
Expand Up @@ -262,6 +262,22 @@ public function testGetsSourceContextFromHtml5File(): void
$this->assertSame("A\nB", $source->getCode());
}

public function testGetsSourceContextFromNestedHtml5File(): void
{
$path = Path::canonicalize(__DIR__.'/../../Fixtures/Twig/legacy/templates');

$loader = $this->getContaoFilesystemLoader(null, new TemplateLocator('/', [], []));
$loader->addPath($path);

$source = $loader->getSourceContext('@Contao/bar.html5');

$this->assertSame('@Contao/bar.html5', $source->getName());
$this->assertSame(Path::join($path, 'bar.html5'), Path::normalize($source->getPath()));

// Block names should be taken from the root template to include all blocks
$this->assertSame("A\nB", $source->getCode());
}

public function testExists(): void
{
$loader = $this->getContaoFilesystemLoader();
Expand Down

0 comments on commit f596595

Please sign in to comment.