8000 [TwigBundle] Added auto namespacing for new templates directory structure by HeahDude · Pull Request #23339 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[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

Closed
wants to merge 2 commits into from

Conversation

HeahDude
Copy link
Contributor
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 todo

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 like app/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:

/templates/bundles/Twig
/templates/bundles/EasyAdmin

What do you think?

@HeahDude HeahDude changed the title Added auto namespacing for new templates directory structure [TwigBundle] Added auto namespacing for new templates directory structure Jun 30, 2017
@linaori
Copy link
Contributor
linaori commented Jul 1, 2017

Looks like a good solution to me.

@HeahDude HeahDude force-pushed the twig/overrides-bundles branch from 73c6a1e to 0980908 Compare July 1, 2017 11:18
@nicolas-grekas nicolas-grekas added this to the 3.4 milestone Jul 3, 2017
}

if ($container->fileExists($dir = $container->getParameter('kernel.project_dir').'/templates/bundles', false)) {
foreach (glob($dir.'/*', GLOB_ONLYDIR) as $path) {
Copy link
Member
@nicolas-grekas nicolas-grekas Jul 3, 2017

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?

Copy link
Contributor Author

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?

Copy link
Member

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.

Copy link
Member
@yceruto yceruto left a 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));
Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Member
@yceruto yceruto Jul 7, 2017

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 👎

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why?

Copy link
Contributor Author

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.

Copy link
Member

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. 👍

Copy link
Member

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.

@JasonCoal
Copy link
JasonCo 8000 al commented Jul 21, 2017 via email

}

if ($container->fileExists($dir = $container->getParameter('kernel.project_dir').'/templates/bundles', false)) {
foreach (glob($dir.'/*', GLOB_ONLYDIR) as $path) {
Copy link
Member

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));
Copy link
Member

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.

@fabpot
Copy link
Member
fabpot commented Jul 27, 2017

@HeahDude Do you time to finish this one?

@HeahDude HeahDude force-pushed the twig/overrides-bundles branch from 0980908 to 851aee6 Compare July 27, 2017 09:50
@HeahDude
Copy link
Contributor Author

@fabpot Sure :) Is 851aee6 going in the right direction?

@@ -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));
Copy link
Member
@yceruto yceruto Jul 27, 2017

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.

Copy link
Member

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"). 

@rosier
Copy link
Contributor
rosier commented Jul 30, 2017

I'm not sure if it's a good idea to auto configure /templates/bundles as the location to place overridden bundle templates. Since it puts the overridden bundle templates in a location that can conflict with the app templates inside the main /templates directory.

You can already place the overridden bundle templates in src/Resources directory without extra config. Which is similar to how the templates are stored inside the bundles and keeps the app templates and the overridden bundle templates separated from each other.

I propose to only keep the default_path config for the main app templates folder part of this PR.

@yceruto
Copy link
Member
yceruto commented Sep 4, 2017

We can make "bundles" directory configurable too? default %twig.default_path%/bundles.

@fabpot
Copy link
Member
fabpot commented Sep 13, 2017

closing in favor of #24179

@fabpot fabpot closed this Sep 13, 2017
fabpot added a commit that referenced this pull request Sep 13, 2017
…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
symfony-splitter pushed a commit to symfony/twig-bundle that referenced this pull request Sep 13, 2017
…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
@HeahDude HeahDude deleted the twig/overrides-bundles branch October 9, 2017 09:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants
0