Tag Pairs
Looping through values.
Tag pairs look similar to single variables.
They are used to fetch specific content and loop through the results, using single variables to control the output.
Tag pairs may contain various parameters and must use a closing tag, indicated by repeating the tag name prefixed with a /
.
{{ entries:listing from="blog" limit="3" }}
<div class="post">
<h2>{{ title }}</h2>
{{ content }}
</div>
{{ /entries:listing }}
The above example will loop through three entries from your blog folder.
For each one, it will print out the div
element between the {{ entries:listing }}
and {{ /entries:listing }}
tags, filling in the single tags with the variables defined in each entry.
With YAML Lists
You can create lists within your YAML. To display information within a YAML list, you will also use a tag pair.
play Watch our screencast on
Variable Tag Pairs
Simple Lists
If you created a simple YAML list — that is, a bulleted, unnamed list — like this:
groceries:
- olives
- vermouth
- frilly toothpicks
Each of your list items will automatically be put in the {{ name }}
tag.
To get them out, you would do this:
<ul>
{{ groceries }}
<li>{{ name }}</li>
{{ /groceries }}
</ul>
Named Lists
You can also create a named-list in YAML, which might look something like this:
friends:
-
name: Jack
url: http://jackmcdade.com
-
name: Fred
url: http://fredhq.com
-
name: Mubs
url: http://mubashariqbal.com/
Note that each item in the list has a set of named variables. Displaying this list in a template would look like this:
<ul>
{{ friends }}
<li><a href="{{ url }}">{{ name }}</a></li>
{{ /friends }}
</ul>