I am doing some customization in my views-view-table.html.twig file i would like to add content.field_output values
My code is
{% set f_v = content.field_output * 1 %}
{{ f_v }}
Or
{% set f_v = content.field_output + 1 %}
{{ f_v }}
Both code are not working
Related
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?
My twig template has for loop, and I am trying to add a varable to it. How do I do this?
The ('var') needs to be the variable > content. + var + .related.buttons
{% include '#PleinCatalog/Theme/includes/themeCatergory.html.twig' with {'var':'something'} %}
{% for button in content.('var').related.buttons %}
{{ button.title }}
{% endfor %}
I have a similar for loop in my projects, where I include templates with some options.
Based on your example - it can be uses like that:
{% for button in content[var].related.buttons %}
…
{% endfor %}
{% for product in products %}
{% set totalPrice = (product.quantity * product.price)|number_format(2, '.', ',') %}
{% endfor %}
{{ totalPrice }}
I need to add the totalPrice value to itself within the loop, to out the total price of the items within the loop.
Is this possible?
Variables in Twig have scopes, therefore first you need to set the variable before loop:
{% set totalPrice = 0 %}
Then make the sum inside the loop:
{% for product in products %}
{% set totalPrice = totalPrice + (product.quantity * product.price) %}
{% endfor %}
And in the and print the sum in proper format:
{{ totalPrice|number_format(2, '.', ',') }}
Twig's set documentation
I am trying to setting a conditional value using if and else if inside a twig template, so that is what I have to do
{% set x = '1' %}
{% if a and b %}
{% set x = '2' %}
{% elseif a or b %}
{% set x = '3' %}
{% else %}
{% set x = '4' %}
{% endif %}
The above works fine for me. But my question is, is there a way I can achieve the same using only one {% %} (do something) block.
Something like:
{%
set x = '1'
if a and b
...
elseif a or b
...
else
%}
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