feature #24179 [TwigBundle] Add default templates directory and option to configure it (yceruto)
This PR was merged into the 3.4 branch.
Discussion
----------
[TwigBundle] Add default templates directory and option to configure it
| Q | A
| ------------- | ---
| Branch? | 3.4
| Bug fix? | no
| New feature? | yes
| BC breaks? | no
| Deprecations? | no
| Tests pass? | yes
| Fixed tickets | -
| License | MIT
| Doc PR | -
Feature freeze is coming so this one should be important for the new structure. Moving forward and alternative of https://github.com/symfony/symfony/pull/23339 but I'm proposing `templates/bundles/<BundleName>` instead of `templates/bundles/<BundleTwigNamespace>` to override bundles templates and easy migration from current `app/Resources/<BundleName>/views` convention. Also this fix the pending comments.
Summary:
* Added new option to configure default path for templates directory:
```yaml
twig:
default_path: '%kernel.project_dir%/templates' # default
```
* Added new path convention to override bundle templates `<default_path>/bundles/<BundleName>`:
```
# Examples:
templates/bundles/TwigBundle/Exception/error.html.twig - @Twig/Exception/error.html.twig
templates/bundles/FOSUserBundle/layout.html.twig - @FOSUser/layout.html.twig
```
Current templates in `<kernel.root_dir>/Resources/<BundleName>/views` have priority over the new one, and both have priority over the bundle `views` path.
Commits
-------
a1b391fb00
Add default templates directory and option to configure it
This commit is contained in:
commit
76ccce7464
@ -6,6 +6,7 @@ CHANGELOG
|
||||
|
||||
* deprecated `Symfony\Bundle\TwigBundle\Command\DebugCommand`, use `Symfony\Bridge\Twig\Command\DebugCommand` instead
|
||||
* deprecated relying on the `ContainerAwareInterface` implementation for `Symfony\Bundle\TwigBundle\Command\LintCommand`
|
||||
* added option to configure default path templates (via `default_path`)
|
||||
|
||||
3.3.0
|
||||
-----
|
||||
|
@ -130,6 +130,10 @@ class Configuration implements ConfigurationInterface
|
||||
->booleanNode('strict_variables')->end()
|
||||
->scalarNode('auto_reload')->end()
|
||||
->integerNode('optimizations')->min(-1)->end()
|
||||
->scalarNode('default_path')
|
||||
->info('The default path used to load templates')
|
||||
->defaultValue('%kernel.project_dir%/templates')
|
||||
->end()
|
||||
->arrayNode('paths')
|
||||
->normalizeKeys(false)
|
||||
->useAttributeAsKey('paths')
|
||||
|
@ -116,7 +116,7 @@ class TwigExtension extends Extension
|
||||
$container->getDefinition('twig.cache_warmer')->replaceArgument(2, $config['paths']);
|
||||
$container->getDefinition('twig.template_iterator')->replaceArgument(2, $config['paths']);
|
||||
|
||||
$bundleHierarchy = $this->getBundleHierarchy($container);
|
||||
$bundleHierarchy = $this->getBundleHierarchy($container, $config);
|
||||
|
||||
foreach ($bundleHierarchy as $name => $bundle) {
|
||||
$namespace = $this->normalizeBundleName($name);
|
||||
@ -137,6 +137,11 @@ class TwigExtension extends Extension
|
||||
}
|
||||
$container->addResource(new FileExistenceResource($dir));
|
||||
|
||||
if (file_exists($dir = $container->getParameterBag()->resolveValue($config['default_path']))) {
|
||||
$twigFilesystemLoaderDefinition->addMethodCall('addPath', array($dir));
|
||||
}
|
||||
$container->addResource(new FileExistenceResource($dir));
|
||||
|
||||
if (!empty($config['globals'])) {
|
||||
$def = $container->getDefinition('twig');
|
||||
foreach ($config['globals'] as $key => $global) {
|
||||
@ -181,7 +186,7 @@ class TwigExtension extends Extension
|
||||
}
|
||||
}
|
||||
|
||||
private function getBundleHierarchy(ContainerBuilder $container)
|
||||
private function getBundleHierarchy(ContainerBuilder $container, array $config)
|
||||
{
|
||||
$bundleHierarchy = array();
|
||||
|
||||
@ -199,6 +204,11 @@ class TwigExtension extends Extension
|
||||
}
|
||||
$container->addResource(new FileExistenceResource($dir));
|
||||
|
||||
if (file_exists($dir = $container->getParameterBag()->resolveValue($config['default_path']).'/bundles/'.$name)) {
|
||||
$bundleHierarchy[$name]['paths'][] = $dir;
|
||||
}
|
||||
$container->addResource(new FileExistenceResource($dir));
|
||||
|
||||
if (file_exists($dir = $bundle['path'].'/Resources/views')) {
|
||||
$bundleHierarchy[$name]['paths'][] = $dir;
|
||||
}
|
||||
|
@ -26,6 +26,7 @@
|
||||
<xsd:attribute name="debug" type="xsd:string" />
|
||||
<xsd:attribute name="strict-variables" type="xsd:string" />
|
||||
<xsd:attribute name="exception-controller" type="xsd:string" />
|
||||
<xsd:attribute name="default-path" type="xsd:string" />
|
||||
</xsd:complexType>
|
||||
|
||||
<xsd:complexType name="date">
|
||||
|
@ -17,6 +17,7 @@ $container->loadFromExtension('twig', array(
|
||||
'charset' => 'ISO-8859-1',
|
||||
'debug' => true,
|
||||
'strict_variables' => true,
|
||||
'default_path' => '%kernel.root_dir%/templates',
|
||||
'paths' => array(
|
||||
'path1',
|
||||
'path2',
|
||||
|
@ -0,0 +1 @@
|
||||
This is a layout
|
@ -0,0 +1 @@
|
||||
This is a layout
|
@ -6,7 +6,7 @@
|
||||
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
|
||||
http://symfony.com/schema/dic/twig http://symfony.com/schema/dic/twig/twig-1.0.xsd">
|
||||
|
||||
<twig:config auto-reload="true" autoescape="true" base-template-class="stdClass" cache="/tmp" charset="ISO-8859-1" debug="true" strict-variables="true">
|
||||
<twig:config auto-reload="true" autoescape="true" base-template-class="stdClass" cache="/tmp" charset="ISO-8859-1" debug="true" strict-variables="true" default-path="%kernel.root_dir%/templates">
|
||||
<twig:form-theme>MyBundle::form.html.twig</twig:form-theme>
|
||||
<twig:global key="foo" id="bar" type="service" />
|
||||
<twig:global key="baz">@@qux</twig:global>
|
||||
|
@ -13,6 +13,7 @@ twig:
|
||||
charset: ISO-8859-1
|
||||
debug: true
|
||||
strict_variables: true
|
||||
default_path: '%kernel.root_dir%/templates'
|
||||
paths:
|
||||
path1: ''
|
||||
path2: ''
|
||||
|
@ -196,6 +196,7 @@ class TwigExtensionTest extends TestCase
|
||||
array(__DIR__.'/Fixtures/Bundle/ChildChildTwigBundle/Resources/views', 'Twig'),
|
||||
array(__DIR__.'/Fixtures/Bundle/ChildTwigBundle/Resources/views', 'Twig'),
|
||||
array(__DIR__.'/Fixtures/Resources/TwigBundle/views', 'Twig'),
|
||||
array(__DIR__.'/Fixtures/templates/bundles/TwigBundle', 'Twig'),
|
||||
array(realpath(__DIR__.'/../..').'/Resources/views', 'Twig'),
|
||||
array(__DIR__.'/Fixtures/Bundle/ChildChildChildChildTwigBundle/Resources/views', 'ChildTwig'),
|
||||
array(__DIR__.'/Fixtures/Bundle/ChildChildChildTwigBundle/Resources/views', 'ChildTwig'),
|
||||
@ -205,6 +206,7 @@ class TwigExtensionTest extends TestCase
|
||||
array(__DIR__.'/Fixtures/Bundle/ChildChildChildTwigBundle/Resources/views', 'ChildChildTwig'),
|
||||
array(__DIR__.'/Fixtures/Bundle/ChildChildTwigBundle/Resources/views', 'ChildChildTwig'),
|
||||
array(__DIR__.'/Fixtures/Resources/views'),
|
||||
array(__DIR__.'/Fixtures/templates'),
|
||||
), $paths);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user