-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[TwigBundle] Added auto namespacing for new templates directory structure #23339
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
Conversation
Looks like a good solution to me. |
73c6a1e
to
0980908
Compare
} | ||
|
||
if ($container->fileExists($dir = $container->getParameter('kernel.project_dir').'/templates/bundles', false)) { | ||
foreach (glob($dir.'/*', GLOB_ONLYDIR) as $path) { |
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the current bundle related logic is in getBundleHierarchy, why doing it another way here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure to get your comment right. Why would we add paths that do not exist? Here we just want to add paths for existing folders that need to override bundles at the application level.
How would you see it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with @nicolas-grekas here. You should add paths for all bundles like done above. The fact that the directory currently exists is not relevant.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Making (by default) the main templates
directory "configurable" is already the fact with Flex/Twig, so, is there a way to detect the main templates directory to do this without introduce a new convention?
@@ -122,6 +122,17 @@ public function load(array $configs, ContainerBuilder $container) | |||
$twigFilesystemLoaderDefinition->addMethodCall('addPath', array($dir)); | |||
} | |||
|
|||
if ($container->fileExists($dir = $container->getParameter('kernel.project_dir').'/templates', false)) { | |||
$twigFilesystemLoaderDefinition->addMethodCall('addPath', array($dir)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In Flex when we're installing the Twig bundle this path will be added twice? AFAIK Twig Filesystem loader doesn't checks duplicated paths.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The goal is to remove that config, if this PR is merged we'll be able to update the recipe.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then this would make it mandatory to use the templates
directory to be able to override templates bundles automatically 👎
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Registering automatically a directory to override bundles templates would require a configur
8000
ation key and this can easily be managed from the TwigExtension, templates/bundles
being then a default if that's really a concern ;).
For now this is also adding the new conventional templates
folder (the bundles
sub directory being a pending proposal), as it was done for app/Ressources/views
a few lines above.
Anyone is still free to add its own paths, even use the new templates directory structure in symfony 2.8 if wanted. Also the addition happens only if the templates
folder exists which should be the new best practice now, we used to assume that app/Resources/views
and app/Resources/*Bundle/views
exist as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I got the point, just that I liked the idea to remove the old convention in favor of explicit configuration. I thought in this in the other way around (add the feature to the configured templates
path) but there is not way to know what is it right now. So I guess rely on the approved structure should be the path. 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of harcoding the templates/
directory here, what about adding a new configuration setting that would default to templates/
and use it here? Should be easy enough.
On Jul 21, 2017, at 1:56 PM, Yonel Ceruto <notifications@github.com> wrote:
@yceruto commented on this pull request.
In src/Symfony/Bundle/TwigBundle/DependencyInjection/TwigExtension.php:
@@ -122,6 +122,17 @@ public function load(array $configs, ContainerBuilder $container)
$twigFilesystemLoaderDefinition->addMethodCall('addPath', array($dir));
}
+ if ($container->fileExists($dir = $container->getParameter('kernel.project_dir').'/templates', false)) {
+ $twigFilesystemLoaderDefinition->addMethodCall('addPath', array($dir));
I got the point, just that I liked the idea to remove the old convention in favor of explicit configuration. I thought in this in the other way around (add the feature to the configured templates path) but there is not way to know what is it right now. So I guess rely on the approved structure should be the path. 👍
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub, or mute the thread.
|
} | ||
|
||
if ($container->fileExists($dir = $container->getParameter('kernel.project_dir').'/templates/bundles', false)) { | ||
foreach (glob($dir.'/*', GLOB_ONLYDIR) as $path) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with @nicolas-grekas here. You should add paths for all bundles like done above. The fact that the directory currently exists is not relevant.
@@ -122,6 +122,17 @@ public function load(array $configs, ContainerBuilder $container) | |||
$twigFilesystemLoaderDefinition->addMethodCall('addPath', array($dir)); | |||
} | |||
|
|||
if ($container->fileExists($dir = $container->getParameter('kernel.project_dir').'/templates', false)) { | |||
$twigFilesystemLoaderDefinition->addMethodCall('addPath', array($dir)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of harcoding the templates/
directory here, what about adding a new configuration setting that would default to templates/
and use it here? Should be easy enough.
@HeahDude Do you time to finish this one? |
0980908
to
851aee6
Compare
@@ -122,12 +122,16 @@ public function load(array $configs, ContainerBuilder $container) | |||
foreach ($bundle['paths'] as $path) { | |||
$twigFilesystemLoaderDefinition->addMethodCall('addPath', array($path, $namespace)); | |||
} | |||
|
|||
$twigFilesystemLoaderDefinition->addMethodCall('addPath', array($config['default_path'].'/bundles/'.$namespace, $namespace)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This path should be added before the bundle path, (here https://github.com/HeahDude/symfony/blob/851aee62eb905c8d1390a12f7f3a56fce5b8abe8/src/Symfony/Bundle/TwigBundle/DependencyInjection/TwigExtension.php#L195):
// ---------> here <---------
if ($container->fileExists($dir = $bundle['path'].'/Resources/views', false)) {
$bundleHierarchy[$name]['paths'][] = $dir;
}
Otherwise we can't able to override templates from third-party bundles, because the parent template will be loaded first always.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also the path needs to be checked if it exists before add it, otherwise Twig throw an exception:
[Twig_Error_Loader]
The "templates/bundles/Twig" directory does not exist ("/home/yceruto/github/symfony/symfony-demo/templates/bundles/Twig").
I'm not sure if it's a good idea to auto configure You can already place the overridden bundle templates in I propose to only keep the default_path config for the main app templates folder part of this PR. |
We can make "bundles" directory configurable too? default |
closing in favor of #24179 |
…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
…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 symfony/symfony#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
Using
app/Resources/views
has never needed any configuration, and it was really easy to override any templates of a bundle by adding a folder for it likeapp/Resources/TwigBundle/views
.Using the new directory structure, every path needs to be added in Twig configuration, which can be handled "automatically" when installing the TwigBundle with Symfony Flex https://github.com/symfony/recipes/blob/master/symfony/twig-bundle/3.3/etc/packages/twig.yaml#L2, but this is not automatic when migrating an app to the new directory strucure.
Even with Flex, overriding templates needs to add an explicit path for each bundle (example https://github.com/EnMarche/en-marche.fr/blob/master/app/config/config.yml#L55).
Moreover, this can be very ugly to have many
*Bundle
among templates folders which most of the time match controllers structure with lower case (see https://github.com/EnMarche/en-marche.fr/tree/master/templates).Thus to ease auto configuration, I propose to place overridden bundle templates in a
bundles
sub folder:What do you think?