Skip to content

[TwigBundle] added support for Twig namespaced paths (Twig 1.10) #5660

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
Oct 5, 2012

Conversation

fabpot
Copy link
Member

@fabpot fabpot commented Oct 3, 2012

In a template, you can now use native Twig template names, instead of
the Symfony ones:

Before (still works):

{% extends "AcmeDemoBundle::layout.html.twig" %}
{% include "AcmeDemoBundle:Foo:bar.html.twig" %}

After:

{% extends "@AcmeDemo/layout.html.twig" %}
{% include "@AcmeDemo/Foo/bar.html.twig" %}

Using native template names is also faster.

The only drawback is that the new notation looks similar to the way we
locate resources in Symfony, which would be
@AcmeDemoBundle/Resources/views/Foo/bar.html.twig. We could have used
the same notation, but it is rather verbose (and by the way, using this
notation did not work anyway in templates).

TODO: update documentation

In a template, you can now use native Twig template names, instead of
the Symfony ones:

Before (still works):

    {% extends "AcmeDemoBundle::layout.html.twig" %}
    {% include "AcmeDemoBundle:Foo:bar.html.twig" %}

After:

    {% extends "@AcmeDemo/layout.html.twig" %}
    {% include "@AcmeDemo/Foo/bar.html.twig" %}

Using native template names is also faster.

The only drawback is that the new notation looks similar to the way we
locate resources in Symfony, which would be
@AcmeDemoBundle/Resources/views/Foo/bar.html.twig. We could have used
the same notation, but it is rather verbose (and by the way, using this
notation did not work anyway in templates).
@fabpot
Copy link
Member Author

fabpot commented Oct 3, 2012

I forgot to mention why I'd like to include this change besides performance: this would allow to share templates between a project using the Symfony2 full-stack framework and any other project using Twig.

@henrikbjorn
Copy link
Contributor

👍 Will the old notation be deprecated at some point?

@stof
Copy link
Member

stof commented Oct 3, 2012

@fabpot does it still support overwriting templates ?

private function addTwigPath($twigLoaderDefinition, $dir, $bundle)
{
$name = $bundle;
if ('Bundle' === substr($name, -6)) {
Copy link
Member

Choose a reason for hiding this comment

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

why removing Bundle ? this is not consistent with the way the @ notation is done elsewhere

Copy link
Member Author

Choose a reason for hiding this comment

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

We are not consistent anyway as we go to Resources/views/ when using the location notation. So, it was done for three main reasons: trying to be as far as possible as the current notation to avoid confusion (or not), removing something that is, by convention, always present anyway, and more importantly trying to be more agnostic for when the templates are used outside of the full-stack framework.

Now, I'm open to any suggestion and if the majority think we need/can keep the bundle suffix, that would fine for me as well.

fabpot added a commit that referenced this pull request Oct 5, 2012
This PR was merged into the master branch.

Commits
-------

5c809d8 [TwigBundle] added support for Twig namespaced paths (Twig 1.10)

Discussion
----------

[TwigBundle] added support for Twig namespaced paths (Twig 1.10)

In a template, you can now use native Twig template names, instead of
the Symfony ones:

Before (still works):

    {% extends "AcmeDemoBundle::layout.html.twig" %}
    {% include "AcmeDemoBundle:Foo:bar.html.twig" %}

After:

    {% extends "@AcmeDemo/layout.html.twig" %}
    {% include "@AcmeDemo/Foo/bar.html.twig" %}

Using native template names is also faster.

The only drawback is that the new notation looks similar to the way we
locate resources in Symfony, which would be
`@AcmeDemoBundle/Resources/views/Foo/bar.html.twig`. We could have used
the same notation, but it is rather verbose (and by the way, using this
notation did not work anyway in templates).

TODO: update documentation

---------------------------------------------------------------------------

by fabpot at 2012-10-03T13:36:56Z

I forgot to mention why I'd like to include this change besides performance: this would allow to share templates between a project using the Symfony2 full-stack framework and any other project using Twig.

---------------------------------------------------------------------------

by henrikbjorn at 2012-10-03T13:50:48Z

:+1: Will the old notation be deprecated at some point?

---------------------------------------------------------------------------

by stof at 2012-10-03T14:29:50Z

@fabpot does it still support overwriting templates ?
@fabpot fabpot merged commit 5c809d8 into symfony:master Oct 5, 2012
@webmozart
Copy link
Contributor

What's the reason for not also supporting this syntax in other parts of the framework, such as the controller?

return $this->renderView('@AcmeDemo/Foo/bar.html.twig', array(...));

@webmozart
Copy link
Contributor

ref #6919

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants