Each Loop Syntax Deep Dive

Setup

You can think of an each statement as being made up of 4 sections. They are the "declaration", the "where", the "sort", and the "limit". The declaration comes first but the other three sections can be in any order. The "where" is by far the most nuanced and will be what we focus most of this article on. We’ll knock out the other three sections first and come back to the filter.

Here is an each loop with some more complex items. Content in parenthesis is placeholder content.

Declaration

The declaration always comes first and has the least complexity. It will always be “{{ each refrence_name as variable” The reference name must match a page group or dataset reference name of an existing set on that Zesty property. The variable must be a string with no spaces or special characters.

Sort

The sort section is written as “sort by variable.field asc/desc” where asc or desc represent ascending or descending sort order. Ascending is assumed if no direction is specified. As a warning, if you reference a field that doesn’t exist to sort by, the page will break. Take note, you can only use the variable.field notation for first field, you can just reference the field name directly.

Limit

Limit is written as “limit 4” or “limit 2,10”. If limit is written with a single number that’s the total number of items it will show. If limit is written with two numbers comma separated, the first number is how many entries to skip and the second number is how many entries to run through the loop. Another way to think about it is “limit 4” is the same thing as “limit 0,4” where either way you would show 4 entries, beginning with the first one.

Where - “where” basics

The where is written as “where something compared to something” and if that condition is true the loop will execute. One of the most common uses would be checking if entries belong on a category page, which might look something like

This loop would execute if the article parent page was set to the current page being viewed.

The Where - Comparison Types

There are several comparison values you can use to generate the each loop with the results you need. They are =, !=, >, >=, <, <=, and like.

Where - and and or

You can set up multiple comparisons on a single each loop by separating them with “and” or “or” or both. Here is an example for an events loop to loop through featured events in the future or events happening today. One thing to note is you can’t use the variable.field notation for fields after an and or an or, you just reference the field name directly. The only exceptions to this are the Zesty auto-included data like “zuid” and "parent_zuid" which must be referenced as “z.zuid” after an “and” or an “or” modifier.

Where - find_in_set()

One of the more complex items you can write in an each loop is using a find_in_set filter. This is used almost exclusively with the one-to-many field type. The syntax is “where find_in_set(target_number, set_of_numbers)” For example a blog article might have many tags associated with it. On the blog page, the loop would look like this:

This is how you would write the loop on the tags page to find related articles.

Where - Syntax notes

When referencing any external variable, including references to parsley variables, {page.field} variables, {site.item} variables or variables established in surrounding each loops, use single french brackets around them. If you are expecting a string, you must wrap the variable in quotes. Additionally the syntax for multiple checks in a where statement is use “variable.field_name” for the first check and just “field_name” for additional clauses. The only exceptions to this are the Zesty auto-included data like “zuid” use “z.zuid” for additional clauses.