twig symfony directory separator - symfony

I use twig in a personal project and would like to access to files as symfony sensiolab with ":" as directory separator. For example :
{% extends '::base.html.twig' %}
{% extends ':default:base.html.twig' %}
Just need to replace ":" by "/" without bundle..
Does someone know how to do this ?

The colons in the template inheritance structure are not to reference directories but resources, the normal format is:
{% extends 'bundle_name:controller:template'%}
The different combinations of the parts will tell Symfony where to find the resource, for example:
{% extends "::base.html.twig" %} means that the base.html.twig template is in the global app resources folder.
{% extends "AcmeDemoBundle::part.html.twig" %}
This means that the template is located inside the Resources folder of the AcmeDemo Bundle.

Related

How can I override **partially** a third-party template?

I need to override a template from third-party bundle by following one of the Symfony's built-in conventions. The Symfony documentation talks about them:
To override the bundle template, just copy index.html.twig template from the bundle to app/Resources/AcmeBlogBundle/views/Blog/index.html.twig (the app/Resources/AcmeBlogBundle directory won't exist, so you'll need to create it). You're now free to customize the template.
You can also override templates from within a bundle by using bundle inheritance. For more information, see How to Use Bundle Inheritance to Override Parts of a Bundle.
While this approach may work, it can be overly complicated if you only need to override a small portion of the template (e.g. some blocks). Additionally, if the third-party bundle updates its own template, your version of the template may become outdated and require updates to stay current with the latest changes.
This is what I've tried to do without success:
{# app/Resources/AcmeBlogBundle/views/Blog/layout.html.twig #}
{% extends '#AcmeBlog/Blog/layout.html.twig' %}
{% block title %}My Default Title{% endblock %}
The above code doesn't work. It breaks after reaching the maximum execution time when I've accessed this page and the clear cache command never ends.
Why it doesn't works and how to achieve it without copying the entire parent template from the third-party bundle?
Related issues and pull-requests without workaround:
https://github.com/symfony/symfony/issues/1966 (2011)
https://github.com/symfony/symfony/pull/2202 (2011)
https://github.com/symfony/symfony-docs/issues/752 (2011) | Unrelated.
https://github.com/twigphp/Twig/issues/1334 (2014)
https://github.com/symfony/symfony/issues/15755 (2015)
https://github.com/symfony/symfony/issues/17054 (2015)
https://github.com/symfony/symfony/issues/17407 (2016)
https://github.com/symfony/symfony/issues/17557 (2016)
Why it doesn't works?
{# app/Resources/AcmeBlogBundle/views/Blog/layout.html.twig #}
{% extends '#AcmeBlog/Blog/layout.html.twig' %}
{% block title %}My Default Title{% endblock %}
The reason comes from Twig's paths and namespaces auto-configuration between bundles, their children and Symfony's path convention. By default Symfony/Bundle/TwigBundle assign the same Twig's namespace (AcmeBlog) for all paths, following this order:
# config.yml
twig:
paths:
# Auto-configuration behind the scenes for TwigBundle extension:
# (1st) if AcmeBlogChildBundle has as parent to AcmeBlogBundle
'src/AcmeBlogChildBundle/Resources/views': AcmeBlog
# (2nd) Path to override bundles (Symfony's convention)
'app/Resources/AcmeBlogBundle/views': AcmeBlog
# (3rd) third-party bundle
'vendor/acme/blog-bundle/Resources/views': AcmeBlog
That means, if you need to render the #AcmeBlog/Blog/layout.html.twig template, Twig try to find it in all paths (in the order has been defined) whose namespace matches AcmeBlog.
So to override it, you had to create this template (/Blog/layout.html.twig) in (1st) or (2nd) paths. But, if you're extending from #AcmeBlog/Blog/layout.html.twig at the same time (thinking about extends from the original template) then Twig performs the same previous procedure, causing a circular template reference (infinite loop) and never reach the (3rd) path.
How to achieve it without copy the entire parent template from the third-party bundle?
Symfony's built-in workaround
Let's define a different Twig's namespace for this third-party bundle in twig paths configuration:
# config.yml
twig:
paths:
# (4th) third-party bundle alias.
# The path can be relative to project or real path
vendor/acme/blog-bundle/Resources/views: AcmeBlogOriginal
Later, use the namespace alias to avoid circular reference when you're overriding a third-party template that extends from the original:
{# app/Resources/AcmeBlogBundle/views/Blog/layout.html.twig #}
{% extends '#AcmeBlogOriginal/Blog/layout.html.twig' %}
{% block title %}My Default Title{% endblock %}
Thus, you're able to override blocks instead the whole template. Even should work out-of-the-box as it's about Twig's paths only.
Since Symfony 3.4 this issue has been solved as built-in feature.
http://symfony.com/blog/new-in-symfony-3-4-improved-the-overriding-of-templates#overriding-and-extending-templates

Symfony bundle inheritance and twig templates overriding

I have a third party bundle called VendorDeliveryBundle.
I want to override one of its twig templates that i called with this syntax in one of my App twig templates:
{% include '#VendorDelivery/Frontend/Booking/_delivery.html.twig' with { 'form': form } only %}
Like this it works, the vendor template is called.
But if i want to override this template by registering the overriding bundle in AppKernel (as described in https://symfony.com/doc/2.8/bundles/inheritance.html) and by creating:
App/DeliveryBundle/Resources/views/Frontend/Booking/_delivery.html.twig
this template doesn't override the vendor template.
But if i use this syntax instead
{% include 'VendorDeliveryBundle:Frontend:Booking/_delivery.html.twig' with { 'form': form } only %}
the template is overridden.
It seems that the # syntax doesn't work as expected.
So i'm wondering if it is a bug or a normal behavior considering this symfony documentation https://symfony.com/doc/2.8/bundles/inheritance.html:
The overriding of resources only works when you refer to resources with the #FOSUserBundle/Resources/config/routing/security.xml method. If you refer to resources without using the #BundleName shortcut, they can't be overridden in this way.
Thanks,
# in twig syntax is a feature called "namespaced paths". (documentation)
Use functionality of {% include 'Bundle:Folder:template' %} it is not the same thing as {% include '#Bundle\Folder\template' %}.
For sample, if you overriding fosub bundle:
{% include '#FOSUser/Security/login.html.twig' %} {# Will be fosub template #}
{% include '#User/Security/login.html.twig' %} {# Will be overrided template #}
{% include 'FOSUserBundle:Security:login.html.twig' %} {# Also will be overrided template #}
Also, i want to add, that if you want to override only a template (without global functionality as controllers, listeners, etc) you can added templates to you app directory. It is well described in this documentation

Symfony 3 : Unable to use Twig in "Create your First Page in Symfony"

I'm trying the example in the tutorial. So I created the necessary twig-file in the right directory.
{# app/Resources/views/lucky/number.html.twig #}
{% extends 'base.html.twig' %}
{% block body %}
<h1>Lucky Numbers: {{ luckyNumberList }}</h1>
{% endblock %}
But if I open the page I get the error
Looks like you try to load a template outside configured directories (../base.html.twig) in lucky\number.html.twig at line 2
Anyone who can help me out?
Firstly remove the ".twig" from "base.html.twig".
Then check that the file "base.html.twig" is at the root of views folder.
In case it is in a nested folder then nested folder is also specified in extends command (check documentation).

How to pass user informations to a template using Symfony2 and FosUserBundle

I'm using symfony2, I installed FosUserBundle and I created a bundle UserBundle as indicated in the FosUserBundle online docs. so far so good.
I also created another controller and I'm able to access the logged user information in this way:
$user = $this->container->get('security.context')->getToken()->getUser();
now imagine that in my website, for all the pages/controller, I need to display some user information, even a simple "Welcome MyUser" at the top of the page (so in base.html.twig). I don't want to replicate the line above in all the controllers, so where is the best place to get this information once and pass them to the base template?
For the example you gave, "Welcome MyUser" , you can get this var in twig template with
{% if is_granted("ROLE") %}
Hi {{ app.user.username }}
{% endif %}
This is, if you don't need logic
Also if you didn't knew it you can use heritage in twig, so that you can create a navbar.html.twig with this fosuser var in it, and then in all your templates do
{% extends "AcmeMyBundle::navbar.html.twig" %}
{% block body %}
....
{% endblock %}

Twig extends short path

Imagine I have a bundle with this views structure:
Ressources
- Views
- Content
- layout.html.twig
- view.html.twig
so the layout is in the same folder like the view
I know I can extend the layout by typing something like :
{% extends "fooBarBundle:Content:layout.html.twig" %}
How would I just reference the current folder?
{% extends "layout.html.twig" %}
It says it can't find it... I'm just asking if this is possible.
Thanks in advance
If you want a shorter syntax, simply move your layout.html.twig to app/Resources/views and call it:
{% extends "::layout.html.twig" %}

Resources