Twig / Symfony2 - using a variable inside array merge - symfony

{% set var_name1 = "hello" %}
{% set var_name2 = "there" %}
{% array1|merge({var_name1: var_name2}) %}
I was hoping the code above would add this to array1:
hello:there
...but it adds:
var_name1:there
I've tried wrapping {{ }} around var_name1. Is it possible to add a record to an array and use a variable for the key?

Enclose the key name in brackets:
{% array1|merge({(var_name1): var_name2}) %}

Note that if var_name1 is a numeric value, it won't work.
You'll have to concat it with a string value :
{% set array1 = array1|merge({(var_name1~'_'): var_name2}) %}

Related

How to use twig object variable with at-sign # inside

I send from Symfony object which contains at-sign # in variable.
Object name is invoice and with {{ dump(invoice) }} in twig template I see object and parameter with path:
invoice[0].banSpojDod#showAs
But I dont know how to get value of this banSpojDod#showAs because there is at-sign #.
Could you help me someone please?
You could try with The attribute function can be used to access a "dynamic" attribute of a variable:
{{ attribute(invoice[0], 'banSpojDod#showAs') }}
Hope this help
Ok thanks. Problem was that I used it in loop, and some parameters not exists. I needed to add exist conditions. So my final code works:
{% for f in invoice %}
{% if attribute(f,'banSpojDod#showAs') is defined %}
{{ attribute(f,'banSpojDod#showAs') }}
{% endif %}
{% endfor %}

Set a Variable with a Twig Macro

I have to set a variable with a macro, that returns a number, so I have this macro:
{% import _self as self %}
{% macro function_size(field) %}
{% import _self as self %}
{# initial Setup #}
{% set field_length = 0 %}
{# Field #}
{% for key, value in field %}
{% if not (key starts with "#") %}
{% set field_length = field_length + 1 %}
{% endif %}
{% endfor %}
{{ field_length }}
{% endmacro %}
The Macro loops through the entries of a field and returns the count of values, that don't start with "#".
So I set a variable with that value:
{% set image_size = self.function_size(content.field_teaser_image) %}
ATTENTION: With this you will set the Variable with Twig Markup. (You can see that when you debug the variable afterwards.)
To get a number/integer as value you have to convert it to a String (that will be interpreted as a number if you calculate with it)
{% set image_size = image_size.__toString %}
With this I set the Variable successfully with a macro.
My Question: Is this a bad practice, are there better ways how to set an Variable?
Thank you!
Two ways to set variables for twig are fine in my opinion.
1. Use theme and other hooks (inside theme or any module) and pass variable from php,
2. create twig extension/filter
Examples:
Filter:
http://leopathu.com/content/create-custom-twig-filter-drupal-8
Extension:
http://symfony.com/doc/current/templating/twig_extension.html

How to use variable as a match in saltstack mine.get

I am trying to use a salt mine to get a list of network interfaces of all the minions with the same os as that of the minion on which the jinja template is rendered.
I am trying something like this:
{% set variable = grains['os'] %}
{% set dict = salt['mine.get'('os:variable','network.interfaces','grain') %}
{% for i in dict : %}
// do stuff here
But the problem is in the above salt will try to match os to the value "variable" not to the actual value of the variable.
Using 'os: {{ variable }}' doesn't work too since {{ x }} just prints the value of variable x.
How can I match against the actual os in this case?
You should try + to concatenate prefix and variable name:
{% set variable = grains['os'] %}
{% set dict = salt['mine.get']('os:' + variable,'network.interfaces','grain') %}
{% for i in dict : %}
# do stuff
{% endfor %}

Set Variable only get the last value in a loop in Symfony Twig

I am very new in Symfony and I have a problem in my twig. I just don't know how to do it :(
{% set trans_number = '' %}
{% for tran in trans %}
{% set trans_number = tran.transNumber %}
{{dump(trans_number)}}
// this is what I get when dumping inside the loop: string(10) "1073110793" string(10) "1073145793" string(12) "646721454679"
{% endfor %}
But when I tried to dump it outside the loop:
{% set trans_number = '' %}
{% for tran in trans %}
{% set trans_number = tran.transNumber %}
{% endfor %}
{{dump(trans_number)}}
// I get only the last value string(12) "646721454679"
Now my question is, how can I access all the values assigned to the trans_number outside the loop?
Thanks in advance.
You're correct with the variable scope and handling the scope access by declaring trans_number before the loop, however you're misinterpreting how set works with Twig, or just how variables work.
When you use set trans_number = tran.transNumber, you're just assigning a new value to the trans_number variable each time. When you move the dump outside of the loop, you're no longer dumping the new value of trans_number through each iteration.
If you wish to build a list of values into trans_number, then you need to initialize trans_number as a list and append to it through each iteration of the for loop:
{% set trans_number = [] %}
{% for tran in trans %}
{% set trans_number = trans_number|merge([tran.transNumber]) %}
{% endfor %}
{{dump(trans_number)}}
For people looking for the answer to the question in the title of the question:
You could use the last Filter
Example:
{{ dump( (trans|last) ) }}
Source: http://twig.sensiolabs.org/doc/filters/last.html

Dynamic twig variable names

Given an array of variables sent to a twig template, such as:
$form = $this->createForm( new ServiceItemType()
, $entity
, array( 'attr'=>
array(
'em' => $this->EM()
,'group' => true
) ) );
I want to capture the variables for easy access in twig. However:
{% for key, value in form.vars.attr %}
{% set key = value %}
{% endfor %}
remaps the key variable in the for loop.
twig objects to:
{% for key, value in form.vars.attr %}
{% set {{key}} = value %}
{% endfor %}
And stack as I am aware never seems to address set. Would anyone who knows, please indicate how to accomplish this variable assignment?
I know this syntax works
{% render "..." with {(key): value} %}
Did you try the following syntax? As of March, Friday 22nd this syntax didn't work so you need to use a work around.
{% set (key) = value %}
An alternative to that would be to include a template and pass and form.vars.attr.
{% include "YourAwesomeBundle:Controller:template.html.twig" with form.vars.attr %}
You can also merge form.vars.attr with another array using the merge function.
{% set vars = {} %}
{% set vars = vars|merge(form.vars.attr) %}
{% include "YourAwesomeBundle:Controller:template.html.twig" with vars %}
Within the included template you will be able to use the variable em and group.

Resources