Reputation: 4679
I have a form that generates field per category, so if I have 5 categories, twig will generate 5 category field.
This array consist of 5 categories, so it will loop 5 times.
{% set categories = ['attraction', 'featured_artist', 'co_presentator', 'major_sponsor', 'minor_sponsor'] %}
I'll count the total category and deduct it by 1 for the use of for loop. We're using in array so it would be like 0,1,2,3.. etc.
{% set total_category = categories|count -1 %}
Now the looping starts here, I have a different column in database, called attraction_image, major_sponsor_image, featured_artist_image. So I have to count the total value in that field per category.
{% for i in 0..total_category %}
<div class="increment-field-container" data-increment-host="{{ categories[i] }}">
{% set total_field = details.attraction_image|explode|count %}
</div>
{% endfor %}
How can I concatenate string as variable,
We're calling the total of field like this
{% set total_field = details.attraction_image|explode|count %}
But I want to happen is,
{% set total_field = details.categories[i]_image|explode|count %}
Technically it would be incorrect:
I tried this String Interpolation thing, but still did not work.
{% set total_field = details."#{categories[i]}"_image|explode|count %}
Any solution to this problem?
Upvotes: 1
Views: 3487
Reputation: 240
I actually came accross this after figuring it out. But the way I got around this issue was as Richard pointed out, using the attribute method:
{% set field_1 = input_get('field_1') %}
{{attribute(blogs, field_1) }}
Whatever you have field_1 se as (for example 'layouts') it would equate to
blogs.layouts
Hope this helps!
Upvotes: 0
Reputation: 4129
You can use the attribute function for this. See my similar answer for details.
Upvotes: 3