V
V
Vladislav Shestakov2020-03-06 01:15:06
Twig
Vladislav Shestakov, 2020-03-06 01:15:06

Is there a way to decompose an array into variables in a Twig template?

Is it possible to do something similar to how in PHP an array is decomposed into variables in Twig?

$info = array('кофе', 'коричневый', 'кофеин');
list($drink, $color, $power) = $info;


My Twig template (fields on ACF, maybe someone knows how to do it better):
{% for photo in tour.get_field('photos') %}
    {% set photo = photo.photo %}
    {% if photo != "" %}
        <img data-lazy='{{photo}}'>
    {% endif %}
{% endfor %}

It turns out that there is an array (repeater-field) with "photos" photos, but it seems to consist of arrays, in which there is one "photo" value that I need. Here's how to optimize it. Maybe there are some ways?

This is how I pass data to the template $context = Timber::context(); $context['tours'] = new Timber\Post();
Globally, I iterate over {% for tour in tours.meta('tours') %} like this.

Any help is welcome.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Yanchevsky, 2020-03-06
@do_while

1. Expand in PHP and put into a separate element in $context?
2. Use some ACF filters to return as needed? https://www.advancedcustomfields.com/resources/acf... I think it can.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question