1

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:

enter image description here

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?

2 Answers 2

3

You can use the attribute function for this. See my similar answer for details.

Sign up to request clarification or add additional context in comments.

1 Comment

While waiting for an answer I'm reading into that documentation, and quite confused on it and here we go, you clarified on how to use it in other question. Thanks a lot! :)
0

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!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.