8000 Add default templates directory and option to configure it · symfony/symfony@a1b391f · GitHub
[go: up one dir, main page]

Skip to content

Commit a1b391f

Browse files
committed
Add default templates directory and option to configure it
1 parent ade060e commit a1b391f

File tree

10 files changed

+25
-3
lines changed

10 files changed

+25
-3
lines changed

src/Symfony/Bundle/TwigBundle/CHANGELOG.md

Lines 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

Lines 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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public function load(array $configs, ContainerBuilder $container)
109109
$container->getDefinition('twig.cache_warmer')->replaceArgument(2, $config['paths']);
110110
$container->getDefinition('twig.template_iterator')->replaceArgument(2, $config['paths']);
111111

112-
$bundleHierarchy = $this->getBundleHierarchy($container);
112+
$bundleHierarchy = $this->getBundleHierarchy($container, $config);
113113

114114
foreach ($bundleHierarchy as $name => $bundle) {
115115
$namespace = $this->normalizeBundleName($name);
@@ -130,6 +130,11 @@ public function load(array $configs, ContainerBuilder $container)
130130
}
131131
$container->addResource(new FileExistenceResource($dir));
132132

133+
if (file_exists($dir = $container->getParameterBag()->resolveValue($config['default_path']))) {
134+
$twigFilesystemLoaderDefinition->addMethodCall('addPath', array($dir));
135+
}
136+
$container->addResource(new FileExistenceResource($dir));
137+
133138
if (!empty($config['globals'])) {
134139
$def = $container->getDefinition('twig');
135140
foreach ($config['globals'] as $key => $global) {
@@ -174,7 +179,7 @@ public function load(array $configs, ContainerBuilder $container)
174179
}
175180
}
176181

177-
private function getBundleHierarchy(ContainerBuilder $container)
182+
private function getBundleHierarchy(ContainerBuilder $container, array $config)
178183
{
179184
$bundleHierarchy = array();
180185

@@ -192,6 +197,11 @@ private function getBundleHierarchy(ContainerBuilder $container)
192197
}
193198
$container->addResource(new FileExistenceResource($dir));
194199

200+
if (file_exists($dir = $container->getParameterBag()->resolveValue($config['default_path']).'/bundles/'.$name)) {
201+
$bundleHierarchy[$name]['paths'][] = $dir;
202+
}
203+
$container->addResource(new FileExistenceResource($dir));
204+
195205
if (file_exists($dir = $bundle['path'].'/Resources/views')) {
196206
$bundleHierarchy[$name]['paths'][] = $dir;
197207
}

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

Lines 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

Lines 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
Lines 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

Lines 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

Lines 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

Lines 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