Override form_end block for one form - symfony

I want to override form_end block but only for one form. My blocks are loaded globally in my config.yml file. For example when i want to override form_row for one field i can make block like this -> formName.field.row.
I was trying to make something like this:
{% block formName_form_end %}
my Code
{% endblock %}
but It doesn't work. Maybe it's just not possible to override form_end using form_name ? I mean that i can only override form_end, but in my case it will affect all forms.

You need form themeing.
{% form_theme form _self %}
{% block form_end %}
my Code
{% endblock %}
{# Rest of template #}
As noted in the documentation, use of _self tells Twig to look for block overrides within the current template file.

Related

Rendering a block in extended -> included template

Problem
In navbar.html.twig I have a block that looks like this:
//navbar.html.twig
{% block back_link %}{% endblock %}
This navbar is included by my base.html.twig.
//base.html.twig
{% include navbar.html.twig %}
And then my page template extends the base.
//page.html.twig
{% extends base.html.twig %}
...
{% block back_link %} Things i want in the navbar. {% endblock %}
But the things I want in the navbar don't show up in the navbar, because it's included by the base, so there's no parent/child relationship there.
Question
What's a good (or any) way to let me override a block in an included template in an extended template?
if you understand your idea right, you want to have some reusable content for a navbar, separated to navbar.html.twig that will possible to use in several templates?
So you can do it with "use" http://twig.sensiolabs.org/doc/tags/use.html
In navbar.html.twig
//navbar.html.twig
{% block back_link %}{% endblock %}
In base.html.twig.
//base.html.twig
{% use '::navbar.html.twig' %}
{{ block('back_link') }}
In page.html.twig
{% extends '::base.html.twig' %}
{% block back_link %} Things i want in the navbar. {% endblock %}

Twig: Extend template !only! on condition? Or dynamicly extend on PHP-side

I've got a twig template:
{% extends base %}
{% block content %}
some content
{% endblock %}
Is there any way to render it without "extending" any template?
If i leave the "base" unset, an error appears.
Something like
{% if base is defined %}
{% extends base %}
{% endif %}
does not work, as far as i know.
Or another option might be, if there is a way to dynamicly define a parent on the php side, like u can do in Smarty:
$smarty->display('extends:layout.tpl|myproject.tpl|mypage.tpl');
Thx!
You can conditionally set template:
{% extends base is defined ? 'main.html.twig' : 'empty.html.twig' %}

Redefining a block initially defined in an included view in the parent view with twig

I have a view A that includes a view B that defines a block Z. Can I have a view C that extends A and redefines block Z?
Need not be said that if i paste B into A (without using an include) it works.
For a practical example:
A is the base of a project.
B is the header of a project that includes a menu.
C is any page of the project.
I define a {% block active %}{% endblock %} for each item of the menu in B, that i want to redefine by {% block active %} class="active" {% endblock %} in C to apply certain styles to the current active menu item.
If this is not the way to go, which is the best workaround/solution?
Unfortunately, it's not possible or it wouldn't be as easy.
Why not bind a variable to the template from the controller and passing it to the nav through the include?
{% include MyAwesomeBundle:Modules:nav.html.twig with {activeItem: activeItem, activeItemClassName: activeItemClassName} %}
An alternative to what you want
{# in base layout template #}
{% block nav %}
{% include MyAwesomeBundle:Modules:nav.html.twig %}
{% endblock %}
{# in page template #}
{% extends base-layout.html.twig %}
{% block nav %}
{% include MyAwesomeBundle:Modules:nav_for_news.html.twig %} {# nav_for_news.html.twig extends nav.html.twig #}
{% endblock %}
This is the closest you can get from what you want
Check out the parent() twig function it's useful.

How to check if a block exists in a twig template - Symfony2

Imagine I have something like this in my twig template
{% block posLeft %}
-----
{%endblock%}
Is there any way to check for existance of the posLeft block without calling to:
block("posLeft")
And check the return value of the posBlock to varify the existance. I am a newbie in Symfony2 + Twig.
You can solve it like this, if you want to display a certain block only if it has content. Hope, this is what you're looking for.
Example index.html.twig
{% set _block = block('dynamic') %}
{% if _block is not empty %}
{{ _block|raw }}
{% endif %}
Example part.html.twig
{% extends "index.html.twig" %}
{% block dynamic %}
Block content goes here.
{% endblock %}
You can do it like this:
{% if block('posLeft') %}
...
{% endif %}
But it is not efficient if you need output of rendered block.
So if you will need block output you should assign it to variable in the first place
and then do assertions
The other answers here do not work for twig 2.1 (I've not tested on ~2.0), so here's a small update:
{% if block('dynamic') is defined %}
{{ block('dynamic')|raw }}
{% endif %}
Note that the line to render the block is not:
{% block dynamic %}
{# this wont work #}
{% endblock %}
This wont work because the block will be rendered during compile, and so the test will return true that it exists (as its tested at runtime). So you need to render the block with {{ block('dynamic')|raw }} instead as this doesn't actually define the block in the template.
First check, which Twig version you are using inside your Symfony project, because the answers here are only for Twig 1.
If you are using Twig 2 you are lucky. According to the Twig documentation, you can use the defined test to check if the block exists in the current template context.
{% if block("dynamic") is defined %}
...
{% endif %}
I have written a little TwigExtension to check if the block gets called inside the if statement and it seems like Twig only really checks if the block exists and does not call it.
The link to the docs: https://twig.symfony.com/doc/2.x/functions/block.html
If you are using Twig 1, the old answer at https://stackoverflow.com/a/13806784/6458657 is still correct.
Twig 2.x
{{ (block("posLeft")) ?? '' }}
If you want to display a block if it's defined or not in one line. Might be a little kludgy but satisfies my needs without a bunch of if..then logic.
Just want to provide another example which worked for me.
<body
{% if block('ngapp') is not empty %}ng-app="{% block ngapp %}{% endblock %}"{% endif %}
>
This allows me in child templates declare {% block ngapp 'myApp' %} and have it displayed within parent.
This was needed because on some pages I was bootstrapping Angular manually via (angular.bootstrap('moduleName', rootElement)) and Angular does not like empty ng-app='' directive and breaks in weird ways.
The accepted answer didn't work for me in Twig 3.3.10, throwing an error that the block wasn't defined.
To solve this and define a block whose contents are conditionally wrapped in a container, only if any block content is set, this worked:
Parent template with block definition and wrapper element
{# parent.twig #}
<h1>Hello. I am the parent.</h1>
{% if block('sidebar') is not empty %}
<div class="sidebar-container">
{% block sidebar %}{% endblock %}
</div>
{% endif %}
Child template setting block content
{% extends 'parent.twig' %}
{% block sidebar %}
Sidebar content from child template
{% endblock %}
Output – block content inside wrapper:
<h1>Hello. I am the parent.</h1>
<div class="sidebar-container">
Sidebar content from child template
</div>
Child template not setting block content
{% extends 'parent.twig' %}
{# sidebar block not set in this one... #}
Output – no empty wrapper element:
<h1>Hello. I am the parent.</h1>

Symfony2 inherit from two form templates

I've set my forms to use a custom template (that produces a DL) and that works fine
twig:
form:
resources:
- 'myBundle:Form:row.html.twig'
But then I want to customise one row of one form and I think I have to do this
{% form_theme edit_form _self, ' %}
{% block _startYear_row %}
{% block form_row %}
{{ parent() }}
<div id="startCalendaryear"></div>
{% endblock %}
{% endblock %}
That make the entire form inherit from _self and loses the customisation of row.html.twig.
How can I set a custom template for all forms while still overriding a single row on one of them?

Resources