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?
Related
Using Timber for Wordpress, I'm trying to loop through an ACF field group and get for each sub_field the value and the label.
My ACF field group is named apartment_specs
I can get the values of each sub_field with:
{% for item in post.get_field('apartment_specs') %}
<li>{{ item }}</li>
{% endfor %}
I can get the labels of each sub_field with:
{% for item in post.field_object('apartment_specs').sub_fields %}
<li>{{ item.name }}</li>
{% endfor %}
I can't really get the two things in the same loop.
Is there in Timber anything like the ACF' get_sub_field_object? How'd it work?
You should use get_sub_field_object function.
Something like this might help you:
{% for key,item in fields.contracts %}
{% set object = function('get_sub_field_object', key) %}
{{ dump(object) }}
<li>{{ object.label }}</li>
{% endfor %}
This is a very simple example. For easier usage you can create Timber function or filter.
Thanks #maxim!
I was able to get what I needed by simply using:
{% for key, item in post.get_field('apartment_specs') %}
<li class="{{key}}">{{item}}</li>
{% endfor %}
I'm trying your solution in another more complex situation
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 %}
First of all I'm learning Twig.
I was wondering if it is possible wit Twig to compare two different values from different arrays/lists?!
I have two list of items I call them like so:
{% if page.cart %}
{% for product in page.cart.products %}
{{ product.id }}
{% endfor %}
{% endif %}
And:
{% if products %}
{% for product in products %}
{{ product.id }}
{% endfor %}
{% endif %}
I want to compare both product.id's so I can create a new statement. Is there any way to compare both values? The idea is to check if an id is present in page.cart.products and if so then do something.
I want to create a new statement to display some info. Something like so:
{% if page.cart %}
{% for product in page.cart.products %}
{% set cartId %}{{ product.id }}{% endset %}
{% endfor %}
{% endif %}
{% if products %}
{% for product in products %}
{% set listId %}{{ product.id }}{% endset %}
{% endfor %}
{% endif %}
{% if cartId == listId %}
.... do this ....
{% endif %}
Any help greatly appreciated!
You can loop over one array and check if the id is present in the second one. If it's there, you can do something.
{# In case you want to store them, you can do so in an array #}
{% set repeatedIds = [] %}
{% for productCart in page.cart.products if page.cart %}
{% for product in products if products %}
{% if productCart.id == product.id %}
<p>This id -> {{ product.id }} is already in page.cart.products</p>
{% set repeatedIds = repeatedIds|merge([product.id]) %}
{% endif %}
{% endfor %}
{% endfor %}
{{ dump(repeatedIds) }}
It's a very basic search algorithm and the cost is quadratic. Obviously, there are more efficient ways to look for an element in an array (though more complicated to implement).
If the amount of products you have to deal with is not very big, you could use this solution. However, if you have, let's say, more than one hundred products in each array (or you feel that the algorithm is slowing down your loading time), you could do this process in the controller using more sophisticated methods and PHP and just pass the result to the template.
Hope it helps.
I have an object called "template.Highlights" where exists 7 items. All items have 2 fields: 'name','desc'. How can I access (without multiples if's) an item through the value of the 'name' field? (the names are: 'hl1', 'hl2', 'hl3', 'hl4', 'hl5', 'hl6', 'hl7').
{% for template.Highlights as highlight %}
{% if highlight.name == 'hl5' %}
{% set item = highlight %}
{% endif %}
{% endfor %}
{{ item.name }}, {{ item.desc }}
Is possible to dynamically call a macro in Twig? For example, here is a template and a macro named "group" which builds a button group using buttons array argument. There are also two other macros, save and delete, for building save and delete buttons.
{# Make a group of buttons #}
{% macro group(buttons) %}
{% spaceless %}
{% import "::macros.html.twig" as macros %}
{% set content = '' %}
{% for button in buttons %}
{% set content = content ~ button %}
{% endfor %}
{{ macros.el('div', content, { 'class' : 'btn-group' }) }}
{% endspaceless %}
{% endmacro %}
{# Make a save button #}
{% macro save(attributes, size, image) %}
{% spaceless %}
{{ _self.primary('save'|trans({}, 'buttons'), attributes, size, image) }}
{% endspaceless %}
{% endmacro %}
{# Make a delete button #}
{% macro delete(attributes, size, image) %}
{% spaceless %}
{{ _self.danger('delete'|trans({}, 'buttons'), attributes, size, image) }}
{% endspaceless %}
{% endmacro %}
This works fine passing an array of buttons:
{% import "::buttons.html.twig" as buttons %}
{% set items = [buttons.save, buttons.delete] %}
{{ buttons.group(items) }}
But i'd like to pass macro names to group macro:
{% import "::buttons.html.twig" as buttons %}
{{ buttons.group(['save', 'delete']) }}
and get save and delete macros called automatically. Is this possible and how?
why not just do
{% import "::buttons.html.twig" as buttons %}
{{ buttons.group([buttons.save, buttons.delete]) }}
For those looking for an example of how to implement dynamic macro calling using attribute, checkout https://gist.github.com/tentacode/9728963b9f3a714608f3