Looping Through Data

Learn how to iterate over lists of data like posts and labels using `<b:loop>`

The `<b:loop>` tag is essential for displaying lists of content. The most common use is within the Blog widget to loop through all posts on a given page, but it can also be used for labels, comments, and more.

The Structure of `<b:loop>`

A <b:loop> tag requires two main attributes: values (the list of data to iterate over) and var (a name for the variable representing one item in each iteration).

Code Examples

xmlLooping Through Post Labels to Create a Tag List
1
2
3
4
5
6
7
8
9
10
<b:if cond='data:post.labels'>
<div class='post-labels'>
<span>Tags:</span>
<b:loop values='data:post.labels' var='label'>
<a expr:href='data:label.url' rel='tag'><data:label.name/></a>
<!-- Add a comma if it's not the last label -->
<b:if cond='!data:label.isLast'>, </b:if>
</b:loop>
</div>
</b:if>

Pro Tip

Inside a loop, you can use the `data:i.isFirst` and `data:i.isLast` (where `i` is your loop variable) booleans to add special styling to the first or last item in a list.

Was this lesson helpful?

Have feedback, found an issue, or have a suggestion? Let us know!