Use Twig Custom Set Variables From an Include - symfony

I'm trying to include a twig file with a bunch of custom set variables and then use the variables in the multiple other template files. Similar to how including a PHP file works.
I don't seem to have access to the variables set inside the include in my index file.
Is there any way to do this?
Sample Code *Edited
Included File:
{# variables.html #}
{% set width = "100" %}
{% set height = "250" %}
Template File:
{# index.html #}
{% include 'variables.html' %}
{{ width }}
{{ height }}
Expected Outcome:
100 250
Actual Outcome:
// Nothing gets output

I was just trying to do the same thing you were and came up with the following:
Created snippets.twig to maintain all these mini variables. In your case, you might call it variables.twig. In this file, I used a macro without any arguments. I was creating formatted entry date markup that I can use across all my templates and it looked like this:
{% macro entry_date() %}
<time datetime="{{post.post_date|date('m-d-Y')}}">{{post.post_date|date('F j, Y')}}</time>
{% endmacro %}
note that the parenthesis after the name declaration were imperative
In my main layout file, layout.twig, I referenced this macro via an import statement so it would be accessible in all child templates:
{% import "snippets.twig" as snippets %}
<!doctype html>
...
In my template files, I now have snippets accessible and can query it like any other variable:
{{ snippets.entry_date }}
UPDATE
This doesn't seem to correctly run code. If you're just storing static content, you should be good. You can also pass args to the macro so I imagine you could make some magic happen there but I haven't tried it.

As far as I know it is only possible with {% extends %} tag. Instead of including template with variables you should extend it.
Example:
variables.tpl:
{% set some_variable='123' %}
... more variables ...
{% block content %}
{% endblock %}
template.tpl
{% extends 'variables.tpl' %}
{% block content %}
{{ some_variable }}
... more code which uses variables assigned in variables.tpl ...
{% endblock %}

You can use a template extension : https://symfony.com/doc/current/templating/twig_extension.html
post|entry_date

Related

Symfony-twig template form theme error

I have everything working correctly and now I'm trying to work with form themes. This is my code to generate the form without a theme.
{% extends 'base.html.twig' %}
{% block body %}
{% include 'menu/menu.html.twig' %}
{% if addpost is defined %}
<div id='add_post_form'>
{{ form_start(addpost) }}
{{ form_widget(addpost) }}
{{ form_end(addpost) }}
</div>
{% endif %}
{% endblock %}
But when I'm adding a form-theme with the following code
{% form_theme form 'form/form_div_layout.html.twig' %}
I get this error:
Variable "form" does not exist
When i execute this without the line, I'm getting the following error:
Unknown "form_help" function. Did you mean "form_rest", "form_end"?
the form_div_layout.html.twig contains the code found at github symfony twig form theme
At my config.yml I've added the following under the twig section,
form_themes:
- 'form/form_div_layout.html.twig'
.
either not, i still have this error
what is missing ???
My file structure
If all your forms are going to use the same theme you only need to add the line in your config, but if you want a particular form theme in a particular template you can use the template tag.
The reason you're getting the 'form is not defined error' is because you don't have a variable called form passed the the template, your form variable is called addpost, so you need to use
{% form_theme addpost 'form/form_div_layout.html.twig' %}

Override form_end block for one form

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.

Difference between {% include '' %} and {{ include('') }} in Twig

It's possible to include a file in two different ways:
{% include 'fic.html.twig' %}
{{ include('fic.html.twig') }}
What is the difference between the two methods?
Source:
http://twig.sensiolabs.org/doc/tags/include.html
http://twig.sensiolabs.org/doc/functions/include.html
Tags are less flexible than functions, for example:
If you want to store contents of a file in a variable if you want to repeat it twice:
{% set content = include('test.twig') %}
Instead of:
{% set content %}
{% include 'test.twig' %}
{% endset %}
If you want to add filters:
{{ include('alert.twig') | upper }}
Its tag equivalent:
{% set temp %}
{% include 'alert.twig' %}
{% endset %}
{{ temp | upper }}
You see, {{ include }} instead of {% include %} will not change the world, but remove some complexity when you need to do tricky stuffs using Twig.
Also, according to the documentation, it is recommended to use {{ include() }} to fit with best practices:
{{ }} is used to print the result of an expression evaluation;
{% %} is used to execute statements.
From Twig's changelog:
* 1.12.0-RC1 (2012-12-29)
* added an include function (does the same as the include tag but in a more flexible way)
I think it's the same functionality, but while {% include '' %} is a tag, the {{ include('') }} is a function. Maybe if you want to overwrite the function it can be easier than a tag.
From Symfony 2.8 (LTS) documentation
2.3 The include() function is available since Symfony 2.3. Prior, the {% include %} tag was used.

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 - twig - how to render a twig template from inside a twig template

I have a xxx.html.twig file which shows a page, but when I want to refresh the page with different data and just update it with new data, I have a select and a submit button for it.
The thing is that I don't know how do I call an action in the controller which I pass parameters to from my twig and call for new data and then I render the same twig template again with new parameters.
How do I do so?
Here are a few different ways:
{{ render(app.request.baseUrl ~ '/helper/test', {"hostid2": hostid } ) }}
or
{% include 'MyCoreBundle:Helper:test.html.twig' with {"hostid2": hostid } only %}
or
{% render controller("MyCoreBundle:Helper:test", {'hostid2': hostid}) %}
Symfony 2.1:
{% render 'YourBundle:YourController:yourAction' with {'var': value} %}
Symfony 2.6+:
{{ render(controller('YourBundle:YourController:yourAction', {'var': value})) }}
And, of course, read the documentation.
I think some parts are depricated here.
To make the include work in latest Symfony 3.1.10, I solved it like this:
{% extends 'base.html.twig' %}
{% block body %}
{{ include('AppBundle:Default:inner_content.html.twig') }}
{% endblock %}
Note: include() with parentheses.
Then all the variables are included from the parent template. If you like to restrict some variables in the child template, you use with ... only (look over)

Resources