Thanks to visit codestin.com
Credit goes to github.com

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

Conversation

yceruto
Copy link
Member

@yceruto yceruto commented Sep 12, 2017

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

@yceruto yceruto changed the base branch from master to 3.4 September 12, 2017 21:24
@nicolas-grekas nicolas-grekas added this to the 3.4 milestone Sep 13, 2017
@mvrhov
Copy link

mvrhov commented Sep 13, 2017

Please no overrides dir. just keep it inside a bundles dir. Does it really matter. The one working on code knows.

@yceruto
Copy link
Member Author

yceruto commented Sep 13, 2017

Changed to bundles directory.

Copy link
Member

@chalasr chalasr left a comment

Choose a reason for hiding this comment

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

We should have a test case for this.

@yceruto yceruto force-pushed the templates branch 2 times, most recently from 80a66f7 to c8e1327 Compare September 13, 2017 13:06
@yceruto
Copy link
Member Author

yceruto commented Sep 13, 2017

Tests added.

@ro0NL
Copy link
Contributor

ro0NL commented Sep 13, 2017

Just a question; should we deprecate the old paths (hardcoded in src).

Ie trigger in case of if (file_exists($dir = $container->getParameter('kernel.root_dir').'/Resources/views')) { etc. Wouldnt that me more concise?

Only if not configured as path already of course.

@fabpot
Copy link
Member

fabpot commented Sep 13, 2017

Thank you @yceruto.

@fabpot fabpot merged commit a1b391f into symfony:3.4 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
@yceruto yceruto deleted the templates branch September 13, 2017 14:17
@fabpot
Copy link
Member

fabpot commented Sep 13, 2017

Just tested on 2 of my projects, and it works perfectly.

@Tobion
Copy link
Contributor

Tobion commented Sep 17, 2017

I agree with @ro0NL that we should deprecate the old /Resources/<BundleName>/views.

  1. it's not good to have different ways for the same thing as makes things inconsistent
  2. there should be a configuration for the new path instead of hardcoding it as it makes things self-explaining, esp. if you add that to the default config file. for the same reason we removed auto-registration of commands in favor of having an explicit import (psr-4 loading and autoconfiguration or not).

@ro0NL
Copy link
Contributor

ro0NL commented Sep 17, 2017

@Tobion at time of posting i didnt realize we need to support both variants; legacy and new projects.

#24227 (comment)

I think also related is we dont want to force users switching to a new structure for no real benefit. But the fact you can mix&match can be confusing for sure! So yes, i think the current approach is a bit too pragmatic.

Technically we're fine :)

@fabpot fabpot mentioned this pull request Oct 19, 2017
xabbuh added a commit to symfony/symfony-docs that referenced this pull request Oct 27, 2017
This PR was merged into the 3.4 branch.

Discussion
----------

Add default_path option reference

symfony/symfony#24179

Commits
-------

66fd357 Minor tweaks
af245fc Add default_path option reference
@nesl247
Copy link

nesl247 commented Dec 28, 2017

Can anyone tell me if this was tested / supposed to work with Form templates? Doesn't seem to be working.

Specifically templates in templates/bundles/BundleName/Form/something.html.twig does not override the original. In my case this is for https://github.com/excelwebzone/EWZRecaptchaBundle but I'm guessing this would apply to any Form templates.

@xabbuh
Copy link
Member

xabbuh commented Jan 2, 2018

@nesl247 please open a new issue if you think you discovered a bug

@nesl247
Copy link

nesl247 commented Jan 3, 2018

Will do. Wasn't sure if I should or not as I'm not sure if it's working as intended or a bug.

@anyt
Copy link

anyt commented Jan 18, 2019

Hi @yceruto,
Am I understand right,
When I have an old reference to the template, with colon notation, it's not possible to override template in a /templates folder by design. Should override be done in src/Resources/.. folder only?
So to use /templates folder to override, the only option I have - rename all the template references?

Asking, because we have more than 2k templates references, and renaming could cause new bugs, but maintaining two folders for override templates at the same time is confusing for developers.

@yceruto
Copy link
Member Author

yceruto commented Jan 18, 2019

When I have an old reference to the template, with colon notation, it's not possible to override template in a /templates folder by design. Should override be done in src/Resources/.. folder only?
So to use /templates folder to override, the only option I have - rename all the template references?

That's correct.

Asking, because we have more than 2k templates references, and renaming could cause new bugs, but maintaining two folders for override templates at the same time is confusing for developers.

Since 4.2 src/Resources/views/ is deprecated but still works as Twig's path and has priority over templates/dir, so first, you can change the notation of the template name without breaking the loading process (thus, changing the loader and making use Twig features), and then, move them to templates/ shouldn't break anything either (it's just an exchange of paths).

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.