Skip to content

Commit

Permalink
Merge branch 'next-16783/template-include-interpolation' into 'trunk'
Browse files Browse the repository at this point in the history
NEXT-16783 - template inheritance with interpolation

See merge request shopware/6/product/platform!7320
  • Loading branch information
ssltg committed Jan 27, 2022
2 parents 121c3c5 + 10cf6bf commit d8e8619
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 3 deletions.
8 changes: 8 additions & 0 deletions changelog/_unreleased/2022-01-26-changed-sw-include-node.md
@@ -0,0 +1,8 @@
---
title: Changed SwInclude Node
issue: NEXT-16783
author: Stefan Sluiter
author_github: ssltg
---
# Core
* Changed `Shopware\Core\Framework\Adapter\Twig\Node\SwInclude` to not resolve namespace of include template because it will be resolved later in the finder.
2 changes: 0 additions & 2 deletions src/Core/Framework/Adapter/Twig/Node/SwInclude.php
Expand Up @@ -15,9 +15,7 @@ protected function addGetTemplate(Compiler $compiler): void
->indent()
->write('$finder = $this->env->getExtension(\'' . NodeExtension::class . '\')->getFinder();')->raw("\n\n")
->write('$includeTemplate = $finder->find(')
->raw('$finder->getTemplateName(')
->subcompile($this->getNode('expr'))
->raw(')')
->raw(");\n\n")
->write('return $this->loadTemplate(')
->raw('$includeTemplate ?? null, ')
Expand Down
16 changes: 15 additions & 1 deletion src/Core/Framework/Test/Adapter/Twig/TwigSwIncludeTest.php
Expand Up @@ -8,6 +8,7 @@
use Shopware\Core\Framework\Adapter\Twig\NamespaceHierarchy\BundleHierarchyBuilder;
use Shopware\Core\Framework\Adapter\Twig\NamespaceHierarchy\NamespaceHierarchyBuilder;
use Shopware\Core\Framework\Adapter\Twig\TemplateFinder;
use Shopware\Core\Framework\Adapter\Twig\TwigEnvironment;
use Shopware\Core\Framework\Test\Adapter\Twig\fixtures\BundleFixture;
use Shopware\Core\Framework\Test\TestCaseBase\KernelTestBehaviour;
use Shopware\Core\Kernel;
Expand All @@ -30,6 +31,19 @@ public function testMultipleInheritance(): void
static::assertSame('innerblockplugin2innerblockplugin1innerblock', $template->render([]));
}

public function testInterpolatedInheritance(): void
{
// order storefront, TestPlugin2, TestPlugin1 is important for this test. Do not change.
$twig = $this->initTwig([
new BundleFixture('Storefront', __DIR__ . '/fixtures/Storefront/'),
new BundleFixture('TestPlugin2', __DIR__ . '/fixtures/Plugins/TestPlugin2'),
new BundleFixture('TestPlugin1', __DIR__ . '/fixtures/Plugins/TestPlugin1'),
]);

$template = $twig->loadTemplate($twig->getTemplateClass('@TestPlugin1/storefront/frontend/include_base.html.twig'), '@TestPlugin1/storefront/frontend/include_base.html.twig');
static::assertSame('--(Textfield base)(Textfield Extend)-(Textfield base)(Textfield Extend)--', $template->render([]));
}

public function testIncludeWithVars(): void
{
$twig = $this->initTwig([
Expand Down Expand Up @@ -111,7 +125,7 @@ private function initTwig(array $bundles): Environment
$loader->addPath($directory, $bundle->getName());
}

$twig = new Environment($loader, ['cache' => false]);
$twig = new TwigEnvironment($loader, ['cache' => false]);

$kernel = $this->createMock(Kernel::class);
$kernel->expects(static::any())
Expand Down
@@ -0,0 +1 @@
{% block foo %}--{% set type = 'textfield' %}{% sw_include '@TestPlugin1/storefront/frontend/' ~ type ~ '.html.twig' %}-{% sw_include '@TestPlugin1/storefront/frontend/textfield.html.twig' %}--{% endblock %}
@@ -0,0 +1 @@
{% block component_field_types_text %}(Textfield base){% endblock %}
@@ -0,0 +1,4 @@
{% sw_extends '@TestPlugin1/storefront/frontend/textfield.html.twig' %}

{% block component_field_types_text %}{{ parent() }}(Textfield Extend){% endblock %}

@@ -0,0 +1 @@
{% block base_content %}dd{% endblock %}

0 comments on commit d8e8619

Please sign in to comment.