1

I have an Matrix field and I want to extract data in the following HTML structure:

<div class="row">
  <div><p>text1</p><img src="{{imgurl1}}" /></div>
  <div><p>text2</p><img src="{{imgurl2}}" /></div>
  <div><p>text3</p><img src="{{imgurl3}}" /></div>
</div>
<div class="row">
  <div><p>text4</p><img src="{{imgurl4}}" /></div>
  <div><p>text5</p><img src="{{imgurl5}}" /></div>
  <div><p>text6</p><img src="{{imgurl6}}" /></div>
</div>

and this is my twig code for retrieving data from craft:


{% for entry in entries %}
  {% if loop.index is divisible by(3) %}
     <div class="row">
  <div><p>text4</p><img src="{{imgurl4}}" /></div>
  <div><p>text5</p><img src="{{imgurl5}}" /></div>
  <div><p>text6</p><img src="{{imgurl6}}" /></div>
</div>
{% else %}
 <div><p>text4</p><img src="{{imgurl4}}" /></div>
  <div><p>text5</p><img src="{{imgurl5}}" /></div>
  <div><p>text6</p><img src="{{imgurl6}}" /></div>
{% endif %}
{% endfor %}

I want to output multiple browse with free divs I am new to Craft and I will appreciate any help, thanks!

1

Not sure if you really wanted dynamic variable names like imgurl1 or not, but something like this?

<div class="row">
{% for entry in entries %}
    {% set loopIndex = loop.index %}
    <div><p>text{{loopIndex}}</p><img src="{{ _context['imgurl' ~ loopIndex] }}" /></div>
    {% if loopIndex is divisible by(3) %}
        {% if not loop.last %}
            </div>
            <div class="row">
        {% endif %}
    {% endif %}
{% endfor %}
</div>
1
  • yes, it displays the correct HTML structure, thank you very much for your help. May 6 '20 at 7:47

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.