Skip to content

Commit 58c980d

Browse files
committed
Fix themes not working with includes
This was due to the switch to ThemeAwareTwigEngine, which is only used when calling the template engine. It basically occurs when rendering templates from controllers (or services), but not when doing includes as Twig calls its inner functions.
1 parent 7aa7db4 commit 58c980d

7 files changed

Lines changed: 203 additions & 208 deletions

File tree

Resources/config/services.yml

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@ services:
77
tags:
88
- { name: kernel.event_subscriber }
99

10-
ez_core_extra.twig_engine:
11-
class: Lolautruche\EzCoreExtraBundle\Templating\ThemeAwareTwigEngine
10+
ez_core_extra.template_name_resolver:
11+
class: Lolautruche\EzCoreExtraBundle\Templating\ThemeTemplateNameResolver
12+
arguments: ["$design;ez_core_extra$"]
13+
lazy: true
14+
15+
ez_core_extra.twig_theme_loader:
16+
class: Lolautruche\EzCoreExtraBundle\Templating\TwigThemeLoader
1217
public: false
13-
decorates: templating.engine.twig
14-
arguments: ["@ez_core_extra.twig_engine.inner"]
15-
calls:
16-
- [setCurrentDesign, ["$design;ez_core_extra$"]]
18+
decorates: twig.loader.filesystem
19+
arguments: ["@ez_core_extra.template_name_resolver", "@ez_core_extra.twig_theme_loader.inner"]
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the EzCoreExtraBundle package.
5+
*
6+
* (c) Jérôme Vieilledent <jerome@vieilledent.fr>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Lolautruche\EzCoreExtraBundle\Templating;
13+
14+
/**
15+
* Interface for template name resolvers.
16+
* A template name resolver will check provided template name and resolve it for current design.
17+
*/
18+
interface TemplateNameResolverInterface
19+
{
20+
const EZ_DESIGN_NAMESPACE = 'ezdesign';
21+
22+
/**
23+
* Resolves provided template name within current design and returns properly namespaced template name.
24+
*
25+
* @param string $name Template name to resolve.
26+
*
27+
* @return string
28+
*/
29+
public function resolveTemplateName($name);
30+
}

Templating/ThemeAwareTwigEngine.php

Lines changed: 0 additions & 99 deletions
This file was deleted.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the EzCoreExtraBundle package.
5+
*
6+
* (c) Jérôme Vieilledent <jerome@vieilledent.fr>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Lolautruche\EzCoreExtraBundle\Templating;
13+
14+
class ThemeTemplateNameResolver implements TemplateNameResolverInterface
15+
{
16+
/**
17+
* @var string Name of the current design, in the current context (e.g. SiteAccess).
18+
*/
19+
private $currentDesign;
20+
21+
/**
22+
* Collection of already resolved template names.
23+
*
24+
* @var array
25+
*/
26+
private $resolvedTemplateNames = [];
27+
28+
public function __construct($currentDesign)
29+
{
30+
$this->currentDesign = $currentDesign;
31+
}
32+
33+
public function resolveTemplateName($name)
34+
{
35+
if (strpos($name, '@'.self::EZ_DESIGN_NAMESPACE) === false) {
36+
return $name;
37+
} elseif (isset($this->resolvedTemplateNames[$name])) {
38+
return $this->resolvedTemplateNames[$name];
39+
}
40+
41+
return $this->resolvedTemplateNames[$name] = str_replace('@'.self::EZ_DESIGN_NAMESPACE, '@'.$this->currentDesign, $name);
42+
}
43+
}

Templating/TwigThemeLoader.php

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the EzCoreExtraBundle package.
5+
*
6+
* (c) Jérôme Vieilledent <jerome@vieilledent.fr>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Lolautruche\EzCoreExtraBundle\Templating;
13+
14+
use Symfony\Bundle\TwigBundle\Loader\FilesystemLoader;
15+
use Twig_ExistsLoaderInterface;
16+
use Twig_LoaderInterface;
17+
18+
/**
19+
* Proxy to regular Twig FilesystemLoader.
20+
* It resolves generic @ezdesign namespace to the actual current namespace.
21+
*
22+
* @note It extends \Symfony\Bundle\TwigBundle\Loader\FilesystemLoader because methods specific to this loader
23+
* (e.g. related to paths and namespaces) are not part of an interface.
24+
*/
25+
class TwigThemeLoader extends FilesystemLoader implements Twig_LoaderInterface, Twig_ExistsLoaderInterface
26+
{
27+
/**
28+
* @var TemplateNameResolverInterface
29+
*/
30+
private $nameResolver;
31+
32+
/**
33+
* @var Twig_LoaderInterface|Twig_ExistsLoaderInterface|\Twig_Loader_Filesystem
34+
*/
35+
private $innerFilesystemLoader;
36+
37+
public function __construct(TemplateNameResolverInterface $templateNameResolver, Twig_LoaderInterface $innerFilesystemLoader)
38+
{
39+
$this->innerFilesystemLoader = $innerFilesystemLoader;
40+
$this->nameResolver = $templateNameResolver;
41+
}
42+
43+
public function exists($name)
44+
{
45+
return $this->innerFilesystemLoader->exists($this->nameResolver->resolveTemplateName($name));
46+
}
47+
48+
public function getSource($name)
49+
{
50+
return $this->innerFilesystemLoader->getSource($this->nameResolver->resolveTemplateName($name));
51+
}
52+
53+
public function getCacheKey($name)
54+
{
55+
return $this->innerFilesystemLoader->getCacheKey($this->nameResolver->resolveTemplateName($name));
56+
}
57+
58+
public function isFresh($name, $time)
59+
{
60+
return $this->innerFilesystemLoader->isFresh($this->nameResolver->resolveTemplateName($name), $time);
61+
}
62+
63+
public function getPaths($namespace = self::MAIN_NAMESPACE)
64+
{
65+
return $this->innerFilesystemLoader->getPaths($namespace);
66+
}
67+
68+
public function getNamespaces()
69+
{
70+
return $this->innerFilesystemLoader->getNamespaces();
71+
}
72+
73+
public function setPaths($paths, $namespace = self::MAIN_NAMESPACE)
74+
{
75+
$this->innerFilesystemLoader->setPaths($paths, $namespace);
76+
}
77+
78+
public function addPath($path, $namespace = self::MAIN_NAMESPACE)
79+
{
80+
$this->innerFilesystemLoader->addPath($path, $namespace);
81+
}
82+
83+
public function prependPath($path, $namespace = self::MAIN_NAMESPACE)
84+
{
85+
$this->innerFilesystemLoader->prependPath($path, $namespace);
86+
}
87+
}

Tests/Templating/ThemeAwareTwigEngineTest.php

Lines changed: 0 additions & 103 deletions
This file was deleted.

0 commit comments

Comments
 (0)