Search code examples
drupaltwigdrupal-themingdrupal-8

Iterate lists/content in block template twig Drupal 8


How would I be able to supersede the hierarchical dependencies in Drupal 8's twig engine to be able to loop within the i.e Lists/Views which is assigned to a block. So we would have a template: block--views-block--[machine-name]-1.html.twig You will be required to have the variable {{ content }}

Which then recursively buries itself down to field templates. Its completely killing me that one would need so many levels to produce on block of content.

I would like to iterate within the top custom block template the list.

Attempted

{% for key, value in _context  %}
    <li>{{ key }}</li>
{% endfor %}

To evaluate what is available to iterate down into the object but with no luck. I did though find a nice overriding object structure to reach the field attributes but that was within the field level

item.content['#item'].entity.uri.value

Thanks


Solution

  • i use this to "generate" a picture from my

    node--news--full.html.twig

    <div class="col-md-3">
        {{ content.field_newsbild }}
    </div>
    

    the twig debug suggests some filenames. i took this:

    field--node--field-newsbild--news.html.twig

    and in there i wrote:

    {% for item in items %}
        <img alt="" src="{{ file_url(item.content['#item'].entity.uri.value) }}" class="img-responsive" {{ attributes }} >
    {% endfor %}
    

    hope i'll help a bit.