8000 [TwigBundle] Add default templates directory and option to configure it by yceruto · Pull Request #24179 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[TwigBundle] Add default templates directory and option to configure it #24179

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension 8000

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Bundle/TwigBundle/CHANGELOG.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
8000
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,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 @@ -130,6 +130,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 @@ -174,7 +179,7 @@ public function load(array $configs, ContainerBuilder $container)
}
}

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

Expand All @@ -192,6 +197,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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is a layout
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is a layout
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
781A
Original file line number Diff line number Diff line change
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