1

I have a block view with an image field.

I know how to theme this field :

views-view-fields—my-images.html.twig

How can I wrap this loop with an element like : <div class="slider-article">

I’ve tried different solutions without success.

Theme the block twig template :

If I use block--views-block--slider-images.html.twig

Of course it doesn’t work because there is no possibility to theme the field inside this twig template.

I’ve tried to add my wrapper in the header and footer option in the view but even if it works, it is not perfect since I need to have a better control on the html (I’m doing a slick slider, I know there is a module but I don’t want to use it).

I’m trying to do something I can do in node :

In node—lorem.html.twig :

{{ content.field_my_images }}

In field--field-my_images.html.twig

<div class="slider-article">

    {% for item in items %}

        <div>

            {{ item.content }}

        </div>

    {% endfor %}

</div>

I’ve also checked all the "rewrite results" options in view but didn’t manage to add a wrapper.

I prefer a solution where I can create my custom html strcucture like the above example with a loop.

EDIT

Here is the HTML structure I need :

<!-- How can I add this wrapper ? -->
<div class="slider-article">
  <!-- views-view-fields--[view-name]--[machine-name].html.twig starts here ! -->
  <div>your content</div>
  <div>your content</div>
  <div>your content</div>
  <!-- END OF views-view-fields--[view-name]--[machine-name].html.twig -->
</div>
1

Option 1

You can by copying (the entire code) from core views-view.html and paste it in your theme's template folder and rename it views-view--[view-name].html.twig and then add your class to this template file.

replace {{ rows }} with

<div class="slider-article">
{{ rows }}
</div>

Option 2

Copy from core views-view-fields and paste it in your theme's template folder, rename it views-view-fields--[view-name].html.twig

Then add your div with class before {% for field in fields -%} and add close tag after {%- endfor %}

Option 3 (maybe)

You can add it via the Views UI by clicking on "None". But this will add the wrapper to the entire view, which you may not want.

enter image description here

| improve this answer | |
  • Hello, Thank for reply but unfortunately it is not what I need. I know how to theme views-view-fields--[view-name]--[machine-name].html.twig (I do it all the time ;-). But here, the wrapper <div class="slider-article"></div> will wrap each fields. I need to wrap the list of images (the loop). I’ve updated my question with the desired html. – Sébastien Gicquel Jun 5 '18 at 7:33
  • @SébastienGicquel I have edited my answer. Hopefully I understood you correctly this time around. – No Sssweat Jun 5 '18 at 8:35
  • 1
    Yes, you understood what I want to do ;-) Option #1 works ! Option #2 adds a wrapper on all fields, option #3 adds a wrapper which could be useful for applying CSS styles but in the case of slick slider, it is not not sufficiently precise because inside this wrapper there are too much html element that make slick slider doesn’t work. – Sébastien Gicquel Jun 5 '18 at 9:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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