Skip to content

Commit

Permalink
feature #24179 [TwigBundle] Add default templates directory and optio…
Browse files Browse the repository at this point in the history
…n 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 #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
-------

a1b391f Add default templates directory and option to configure it
  • Loading branch information
fabpot committed Sep 13, 2017
2 parents 005cf6b + a1b391f commit 76ccce7
Show file tree
Hide file tree
Showing 10 changed files with 25 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/Symfony/Bundle/TwigBundle/CHANGELOG.md
Expand Up @@ -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
-----
Expand Down
Expand Up @@ -130,6 +130,10 @@ private function addTwigOptions(ArrayNodeDefinition $rootNode)
->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')
Expand Down
Expand Up @@ -116,7 +116,7 @@ public function load(array $configs, ContainerBuilder $container)
$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);
Expand All @@ -137,6 +137,11 @@ public function load(array $configs, ContainerBuilder $container)
}
$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) {
Expand Down Expand Up @@ -181,7 +186,7 @@ public function load(array $configs, ContainerBuilder $container)
}
}

private function getBundleHierarchy(ContainerBuilder $container)
private function getBundleHierarchy(ContainerBuilder $container, array $config)
{
$bundleHierarchy = array();

Expand All @@ -199,6 +204,11 @@ private function getBundleHierarchy(ContainerBuilder $container)
}
$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;
}
Expand Down
Expand Up @@ -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">
Expand Down
Expand Up @@ -17,6 +17,7 @@
'charset' => 'ISO-8859-1',
'debug' => true,
'strict_variables' => true,
'default_path' => '%kernel.root_dir%/templates',
'paths' => array(
'path1',
'path2',
Expand Down
@@ -0,0 +1 @@
This is a layout
@@ -0,0 +1 @@
This is a layout
Expand Up @@ -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>
Expand Down
Expand Up @@ -13,6 +13,7 @@ twig:
charset: ISO-8859-1
debug: true
strict_variables: true
default_path: '%kernel.root_dir%/templates'
paths:
path1: ''
path2: ''
Expand Down
Expand Up @@ -196,6 +196,7 @@ public function testTwigLoaderPaths($format)
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'),
Expand All @@ -205,6 +206,7 @@ public function testTwigLoaderPaths($format)
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);
}

Expand Down

0 comments on commit 76ccce7

Please sign in to comment.