Data Repeat
The data-repeat attribute generates repeated HTML from arrays in your data. Write the template once and Cook stamps it out for each item at build time.
Basic usage
Add data-repeat to any element. The value specifies which array to iterate and a name for the current item:
export default { features: [ { title: 'Fast Builds', desc: 'Sub-second builds for most sites' }, { title: 'Zero JS', desc: 'No client-side framework required' }, { title: 'Markdown', desc: 'Write content in Markdown with frontmatter' }, ],};
<ul> <li data-repeat="features as feature"> <strong>${feature.title}</strong> — ${feature.desc} </li></ul>
Cook outputs one <li> for each item, with template strings resolved:
Nested data access
Use dot notation to reference arrays inside nested objects:
<div data-repeat="company.team as member"> <h3>${member.name}</h3> <p>${member.role}</p></div>
Limit and offset
Control how many items to render and where to start with pipe modifiers:
<article data-repeat="posts | limit:3 as post"> <h2>${post.title}</h2></article>
<article data-repeat="posts | offset:2 | limit:5 as post"> <h2>${post.title}</h2></article>
Offset is applied first, then limit. So offset:2 | limit:5 skips the first 2 items and takes the next 5.
Index variable
Access the current iteration index by adding a second variable name after a comma:
<ol> <li data-repeat="steps as step, i"> Step ${i}: ${step.text} </li></ol>
The index is zero-based.
Template strings in attributes
Template variables work in attributes on the repeated element itself, not just in its children:
<a data-repeat="posts as post" href="${post.url}" class="post-link"> <h2>${post.title}</h2> <time>${post.date}</time></a>
Using with collections
Markdown collections built during the build process are available through the collections namespace. If you have Markdown files in src/blog/, Cook auto-generates a collections.blog array:
<article data-repeat="collections.blog as post"> <h2><a href="${post.url}">${post.title}</a></h2> <p>${post.description}</p></article>
Collection items have properties from their front matter (title, description, date, etc.) plus a url field generated by Cook.
Using with components
Repeat runs before component expansion in the build pipeline. This means you can repeat a custom element and each copy gets expanded individually:
<product-card data-repeat="products as product" name="${product.name}" description="${product.desc}" href="${product.url}"></product-card>
Each repeated <product-card> is expanded into its full component template with the correct attribute values.
Expression syntax
The full expression format is:
dataPath | offset:N | limit:N as itemName, indexName
dataPath-- dot-notation path to an array (e.g.,features,collections.blog)offset:N-- optional, skip the first N itemslimit:N-- optional, take at most N itemsitemName-- required, variable name for the current itemindexName-- optional, variable name for the zero-based index
The data-repeat attribute is removed from the output by default.