Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 76ccce7

Browse filesBrowse files
committed
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 #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
2 parents 005cf6b + a1b391f commit 76ccce7
Copy full SHA for 76ccce7

File tree

10 files changed

+25
-3
lines changed
Filter options

10 files changed

+25
-3
lines changed

‎src/Symfony/Bundle/TwigBundle/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/TwigBundle/CHANGELOG.md
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ CHANGELOG
66

77
* deprecated `Symfony\Bundle\TwigBundle\Command\DebugCommand`, use `Symfony\Bridge\Twig\Command\DebugCommand` instead
88
* deprecated relying on the `ContainerAwareInterface` implementation for `Symfony\Bundle\TwigBundle\Command\LintCommand`
9+
* added option to configure default path templates (via `default_path`)
910

1011
3.3.0
1112
-----

‎src/Symfony/Bundle/TwigBundle/DependencyInjection/Configuration.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/TwigBundle/DependencyInjection/Configuration.php
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,10 @@ private function addTwigOptions(ArrayNodeDefinition $rootNode)
130130
->booleanNode('strict_variables')->end()
131131
->scalarNode('auto_reload')->end()
132132
->integerNode('optimizations')->min(-1)->end()
133+
->scalarNode('default_path')
134+
->info('The default path used to load templates')
135+
->defaultValue('%kernel.project_dir%/templates')
136+
->end()
133137
->arrayNode('paths')
134138
->normalizeKeys(false)
135139
->useAttributeAsKey('paths')

‎src/Symfony/Bundle/TwigBundle/DependencyInjection/TwigExtension.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/TwigBundle/DependencyInjection/TwigExtension.php
+12-2Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public function load(array $configs, ContainerBuilder $container)
116116
$container->getDefinition('twig.cache_warmer')->replaceArgument(2, $config['paths']);
117117
$container->getDefinition('twig.template_iterator')->replaceArgument(2, $config['paths']);
118118

119-
$bundleHierarchy = $this->getBundleHierarchy($container);
119+
$bundleHierarchy = $this->getBundleHierarchy($container, $config);
120120

121121
foreach ($bundleHierarchy as $name => $bundle) {
122122
$namespace = $this->normalizeBundleName($name);
@@ -137,6 +137,11 @@ public function load(array $configs, ContainerBuilder $container)
137137
}
138138
$container->addResource(new FileExistenceResource($dir));
139139

140+
if (file_exists($dir = $container->getParameterBag()->resolveValue($config['default_path']))) {
141+
$twigFilesystemLoaderDefinition->addMethodCall('addPath', array($dir));
142+
}
143+
$container->addResource(new FileExistenceResource($dir));
144+
140145
if (!empty($config['globals'])) {
141146
$def = $container->getDefinition('twig');
142147
foreach ($config['globals'] as $key => $global) {
@@ -181,7 +186,7 @@ public function load(array $configs, ContainerBuilder $container)
181186
}
182187
}
183188

184-
private function getBundleHierarchy(ContainerBuilder $container)
189+
private function getBundleHierarchy(ContainerBuilder $container, array $config)
185190
{
186191
$bundleHierarchy = array();
187192

@@ -199,6 +204,11 @@ private function getBundleHierarchy(ContainerBuilder $container)
199204
}
200205
$container->addResource(new FileExistenceResource($dir));
201206

207+
if (file_exists($dir = $container->getParameterBag()->resolveValue($config['default_path']).'/bundles/'.$name)) {
208+
$bundleHierarchy[$name]['paths'][] = $dir;
209+
}
210+
$container->addResource(new FileExistenceResource($dir));
211+
202212
if (file_exists($dir = $bundle['path'].'/Resources/views')) {
203213
$bundleHierarchy[$name]['paths'][] = $dir;
204214
}

‎src/Symfony/Bundle/TwigBundle/Resources/config/schema/twig-1.0.xsd

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/TwigBundle/Resources/config/schema/twig-1.0.xsd
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
<xsd:attribute name="debug" type="xsd:string" />
2727
<xsd:attribute name="strict-variables" type="xsd:string" />
2828
<xsd:attribute name="exception-controller" type="xsd:string" />
29+
<xsd:attribute name="default-path" type="xsd:string" />
2930
</xsd:complexType>
3031

3132
<xsd:complexType name="date">

‎src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/Fixtures/php/full.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/Fixtures/php/full.php
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
'charset' => 'ISO-8859-1',
1818
'debug' => true,
1919
'strict_variables' => true,
20+
'default_path' => '%kernel.root_dir%/templates',
2021
'paths' => array(
2122
'path1',
2223
'path2',
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is a layout
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is a layout

‎src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/Fixtures/xml/full.xml

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/Fixtures/xml/full.xml
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
77
http://symfony.com/schema/dic/twig http://symfony.com/schema/dic/twig/twig-1.0.xsd">
88

9-
<twig:config auto-reload="true" autoescape="true" base-template-class="stdClass" cache="/tmp" charset="ISO-8859-1" debug="true" strict-variables="true">
9+
<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">
1010
<twig:form-theme>MyBundle::form.html.twig</twig:form-theme>
1111
<twig:global key="foo" id="bar" type="service" />
1212
<twig:global key="baz">@@qux</twig:global>

‎src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/Fixtures/yml/full.yml

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/Fixtures/yml/full.yml
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ twig:
1313
charset: ISO-8859-1
1414
debug: true
1515
strict_variables: true
16+
default_path: '%kernel.root_dir%/templates'
1617
paths:
1718
path1: ''
1819
path2: ''

‎src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/TwigExtensionTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/TwigExtensionTest.php
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ public function testTwigLoaderPaths($format)
196196
array(__DIR__.'/Fixtures/Bundle/ChildChildTwigBundle/Resources/views', 'Twig'),
197197
array(__DIR__.'/Fixtures/Bundle/ChildTwigBundle/Resources/views', 'Twig'),
198198
array(__DIR__.'/Fixtures/Resources/TwigBundle/views', 'Twig'),
199+
array(__DIR__.'/Fixtures/templates/bundles/TwigBundle', 'Twig'),
199200
array(realpath(__DIR__.'/../..').'/Resources/views', 'Twig'),
200201
array(__DIR__.'/Fixtures/Bundle/ChildChildChildChildTwigBundle/Resources/views', 'ChildTwig'),
201202
array(__DIR__.'/Fixtures/Bundle/ChildChildChildTwigBundle/Resources/views', 'ChildTwig'),
@@ -205,6 +206,7 @@ public function testTwigLoaderPaths($format)
205206
array(__DIR__.'/Fixtures/Bundle/ChildChildChildTwigBundle/Resources/views', 'ChildChildTwig'),
206207
array(__DIR__.'/Fixtures/Bundle/ChildChildTwigBundle/Resources/views', 'ChildChildTwig'),
207208
array(__DIR__.'/Fixtures/Resources/views'),
209+
array(__DIR__.'/Fixtures/templates'),
208210
), $paths);
209211
}
210212

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.