Skip to content
Grav 2.0 is officially stable. Read the announcement →
Themes & Styling

For loop in another for loop

Solved by Jared View solution

Started by mikael 9 years ago · 2 replies · 849 views
9 years ago

<div class="card-group transp">
{% for mainsubject in page.header.mainsubjects %}
<div class="card transp">
<div class="card-block">
<h4 class="card-title">{{ mainsubject.title }}</h4>
<p class="card-text">{{ mainsubject.text }}</p>
<ul class="list-group list-group-flush">
{% for link in page.header.mainsubjects.links %}
<li class="list-group-item transp"><a href="{{ link.title }}" class="card-link">{{ link.title }}</a></li>
{% endfor %}
</ul>
</div>
</div>
{% endfor %}
</div>

Frontmatter:

YAML
mainsubjects:
    -
         title: title
         text: test
         links:
            -
                title: test
                link: http://www.test.com
            -
                title: test
                link: http://www.test.com
    -
         title: title
         text: test
         links:
            -
                title: test
                link: http://www.test.com
            -
                title: test
                link: http://www.test.com

Hi! Cannot get the link to list within each mainsubject. The first for loop works good, just not the for loop inside...

Appriciate all help!

Thanks!

👍 1
last edited 07/25/17 by mikael
9 years ago Solution

Hi
I've done something similar but can't seem to find my example so kinda just thinking out loud here but I think you can use the first loop for the second loop- something like
{% for link in mainsubject.links %}
because I think as it is you'll get all 4 links coming through at once - no?

👍 1
9 years ago

Wow! Thank you very much! I did think of the option it would read directly through the first loop :) haha so happy! This is much simpler then i expected!
This works great! So you are right, if you create a loop of any data set then the loop of datasets inside that would not need where to go other then the second loop.

TWIG
   <div class="card-group transp">
  {% for mainsubject in page.header.mainsubjects %}
  <div class="card transp">
    <div class="card-block">
      <h4 class="card-title">{{ mainsubject.title }}</h4>
      <p class="card-text">{{ mainsubject.text }}</p>
      <ul class="list-group list-group-flush">
        {% for link in mainsubject.links %}
        <li class="list-group-item transp"><a href="{{ link.title }}" class="card-link">{{ link.title }}</a></li>
        {% endfor %}
      </ul>
    </div>
  </div>
  {% endfor %}
</div>

Thank you again!

Suggested topics

Topic Participants Replies Views Activity
Themes & Styling · by Sebadamus, 6 days ago
5 148 14 hours ago
Themes & Styling · by martynfoster735, 1 week ago
1 192 1 week ago
Themes & Styling · by Justin Young, 3 weeks ago
1 241 3 weeks ago
Themes & Styling · by Slebeig, 1 month ago
4 376 1 month ago
Themes & Styling · by Pedro M, 3 months ago
4 745 3 months ago