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" %}
Related
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
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).
I extend FosUserBundle register form like this:
{% extends "#App/base.html.twig" %}
{% form_theme form 'bootstrap_3_horizontal_layout.html.twig' %}
{% block form_label_class -%}
col-md-8
{%- endblock form_label_class %}
Unfortunately the block form_label_class which is from bootstrap_3_horizontal_layout.html.twig is not extending there.
I want now in my form to use label as col-md-8 (instead the default col-sm-2) but then in another form maybe want to use col-md-6 and so on.
Is there an easy way to do it inline in every from instead create extends for each col-md-XX which is really not very convenient at all.
Thanks a lot!
You need to tell Symfony to include your current template as a form theme:
{% form_theme form with [
'bootstrap_3_horizontal_layout.html.twig',
_self,
] %}
Otherwise, it doesn't know to look in the current template for form theme blocks.
You can also use this to include other templates with form blocks as well. This is helpful if you have multiple templates that re-use the same form blocks; you can refactor them all into one template and reference it everywhere it's needed.
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.
I'm thinking to organize my base layout in symfony2 containing only 3 blocks: header, content, and footer. And I want to have one template for each block. The "content" template will be a template that will be empty, showing only the templates for every section, following the "3 levels" directives.
But I don't know how to include the header and footer template. I've done it creating "by pass" templates, so, for example, content extends footer, footer extends header, and header extends base, but it looks very bad.
Thanks.
you can use the embed tag, that combines the behaviour of include and extends. It allows you to include another template's contents, just like include does. But it also allows you to override any block defined inside the included template, like when extending a template,
but version 1.8 is required
The embed tag
You can't extend more than one template in Twig, it is illogical anyway.
You should use include, which is a bit different.
The common way is to have one base template, which will be extended by all the other ones, except the header and the footer that will be included in it.
base.html.twig:
...
<body>
{% include '::header.html.twig' %}
{% block body %}{% endblock %}
{% include '::footer.html.twig' %}
</body>
...
In the other templates, your bundles' views for example:
{% extends '::base.html.twig' %}
{% block body %}
Hello world!
{% endblock %}