Variables inside field names

Support MB Views Variables inside field namesResolved

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #33107
    AMXAMX
    Participant

    Hi,

    due to certain limitations of groups and cloneable fields, I had to create multiple rows/series of regular fields. The pattern is more or less like this:

    'series_1_some_field', 'series_1_some_other_field', (...)
    'series_2_some_field', 'series_2_some_other_field', (...)

    Now I am building a template for an HTML table with all these series of these fields.
    I would like to use the iteration (series) number inside the field name, so that I don't have to repeat the same thing over and over again.

    Something like that:

    {% for i in 1..20 %}
    
    <tr role="row">
          <td role="cell"> {{ i }} </td>
          <td role="cell"> {{ post.series_[~i~]_some_field }}</td>
          <td role="cell"> {{ post.series_[~i~]_some_other_field }} </td>
    </tr>
    
    {% endfor %}

    I found the 'bracket notation' in Drupal forums, and tried a few variations,
    like {{post.[series_~i~_some_field] }} etc. But I cannot make it work, and the official Twig documentation is rather short and doesn't have too many examples.

    I know it's super easy with cloneable fields, and I use them normally, but in this case I can't use them.

    Is it possible to pass the iteration number inside the field name?

    Greetings,
    Tom

    #33124
    Long NguyenLong Nguyen
    Moderator

    Hi Tom,

    It's a rare case, I've not used this before. You can follow this topic to know how to access the dynamic variable name in Twig https://stackoverflow.com/questions/24697313/how-to-access-dynamic-variable-names-in-twig

    #33169
    AMXAMX
    Participant

    The methods with attribute or _context mentioned there somehow didn't work for me.

    But I used the |join filter to construct the field id as a variable in a loop, and then mb.rwmb_meta(var) to display its value. It worked.

    Greetings,
    Tom

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.