Getting a specific value from Twig array (using Symfony2) - symfony

So I can do this:
{% for x in data %}
{{ x.label }}: {{ x.value }}<br />
{% endfor %}
But I want to do this kind of thing to get one specific value:
{{ data['label' }}
I can't see how to do it, but it must be possible.

http://twig.sensiolabs.org/doc/functions/attribute.html
{{ attribute(data,'label') }}

Related

Group inside Repeater ACF Pro WordPress Timber

I have checked the documentation and various posts and could not find examples of this.
What I want is to access a field of a group, which is within a repeater in ACF Pro, using Timber/Twig.
What I have is:
{% for item in post.meta('main_field') %}
{{ item.normal_field }} // outputs fine
{% for group_name in item.main_field %}
{{ group_name.group_field }} // outputs nothing
{% endfor %}
{% for group_name in item %}
{{ group_name.group_field }} // outputs only the first instance
{% endfor %}
{% endfor %}
If I put {{ item.group_name.group_field }} I get only the data from the first item output.
I have also tried adding {% set group = post.meta('group_name') %} both before and after {% for item in post.meta('main_field') %} which outputs nothing, when referenced by {{ group.group_field }}.
So how do you reference a group within a repeater?

Twig - passing a parameter to a function with a dynamic variable name

I have Twig variables:
{{ totalAmountMonth0 }} ... {{ totalAmountMonth10 }}
I have a loop and I want to call this variable for example:
{{ totalAmountMonth5 }}
I want to give this variable to a function like this:
totalAmount.percentFromTotalAmount((totalAmountMonth5))
But this doesn't work:
{% for i in 0..10 %}
{{ totalAmount.percentFromTotalAmount(totalAmountMonth~i) }}
{% endfor %}
This doesn't work either:
{% for i in 0..10 %}
{{ totalAmount.percentFromTotalAmount('totalAmountMonth'~i) }}
{% endfor %}
Untested, but try this:
{% for i in 0..10 %}
{{ totalAmount.percentFromTotalAmount(attribute(_context, 'totalAmountMonth'~i)) }}
{% endfor %}
Just provide to your twig an array, which I suggest, or build it (see below example)
// use line below only if array isn't provided to twig
{% set totalAmounts = { totalAmount1, totalAmount2, ..., totalAmount10 } %} // pseudo-code; you need to declare all variables here
{% for ta in totalAmounts %}
{{ totalAmount.percentFromTotalAmount(ta) }}
{% endfor %}
Try to set that variable before and use it.
{% for i in 0..10 %}
{% set temp = 'totalAmountMonth'~i %}
{{ totalAmount.percentFromTotalAmount(temp) }}
{% endfor %}

Looping in twig

How can I echo 1, 2, 3, 4, .... with a twig counter? I can accomplish it with dirty code below but is there a better way?
{% set i = 0 %}
{% for brand in brands %}
{% set i = i + 1 %}
{{ i }}
{% endfor %}
I used {{ cycle(["even", "odd"], loop.index) }} but only getting even or odd.
Also checked Twig docs and range
You can use the loop.index0 or loop.index variable, as mentioned in the docs.
{% for brand in brands %}
{{ loop.index }}
{% endfor %}
Just use {{ loop.index }} or {{ loop.index0 }}if you need it to be 0 indexed

Symfony2 presenting an array value as a translated text

it does not seem possible to use a value i am getting right from an array, as a translated text....so when i have
{% for key,value in ratings %}
<th scope="row">
{% trans %}
{{ value.type }}
{% endtrans %}</th>
<td ><div class="rating" id= "{{ value.type }}" data-rating="{{ value.ratingaverage }}"
thread="{{thread_id}}" rating_readonly= "{{ value.readOnly }}" route="{{ path('addrating') }}" ></div> </td>
{% endfor %}
i get the error
A message must be a simple text in TrackerMembersBundle::rating.html.twig at line 92
what is meant here, is the line with
{% trans %}
{{ value.type }}
{% endtrans %}
i cannot seem to be able to use trans upon the value coming directly from array? the value would be for example "file.quality"
Perhaps its better when you search here before.
Symfony2+Twig, variable in translation return "A message must be a simple text"
The mistake is the variable in the translation block. You have to set an placeholder and replace this with a value.
{% trans with {'%type%':value.type} %}
This is my %type%!
{% endtrans %}

Reference a variable inside expression in Twig

I am displaying a form with Symfony2 and Twig, and need to display some fields in a dynamic order using a loop over an array. The code looks like this:
{% for activity in activities %}
<div class="check">
{{ form_label(tags_form.chactivity{{ activity.id }}) }}
{{ form_widget(tags_form.chactivity{{ activity.id }}) }}
</div>
{% endfor %}
But of course the {{ activity.id }} does not fit here.
How can I use the activity.id (integer) inside the {{ form_label(...) }} and {{ form_widget(...) }} expressions?
You can do this using the attribute function:
{{ form_label(attribute(tags_form, 'chactivity' ~ activity.id)) }}

Resources