2

I am working on a field template to display images in a carousel (I'm using BootStrap). My field is named “galerie”
I copied field.html.twig from the core and saved it as field--field-galerie.html.twig
I managed successfuly to add the necessary markup for BootStrap carousel to work.

The wrapping markup displaying the carousel and its control shows up when there is only one image. I thought it would not. It opens after a {% if multiple %} condition and closes before the {% else %}.
My understanding is that {% if multiple %} only checks if the field is set to accept multiple values.

I don't know how to write an condition in twig checking if the field contains one image or more, to trigger (or not) the carousel thingee.

1 Answer 1

3

Use the length filter, which also works on arrays:

{% if items|length > 1 %}
  field contains more than 1 item
{% endif %}
1
  • Thank you, it was exacltly what I needed. I 'm using it right now.
    – Mars
    Commented Jan 25, 2018 at 18:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.