Template-based list rendering with safe data binding. Fetch or inline JSON, bind to templates, and lay out the cards with the global data-layout system.
Overview
The <card-list> component renders a list of items from JSON data using an HTML <template>. Data binding uses data-field attributes — only safe property paths are allowed, no expressions or method calls.
Supply data inline via data-items, fetch from a URL via src, or assign the .items property. Lay the cards out with the global data-layout system (grid, stack, reel, …) — data-layout-gap and data-layout-min work as on any other element.
123456789101112131415161718
<card-list data-layout="grid" data-items='[ {"id": 1, "name": "Headphones", "price": "$249", "inStock": true}, {"id": 2, "name": "Keyboard", "price": "$159", "inStock": true}, {"id": 3, "name": "Monitor", "price": "$449", "inStock": false}]' data-key="id"> <template> <layout-card> <header><h3 data-field="name"></h3></header> <section> <p data-field="price"></p> </section> <footer> <button data-field-if="inStock">Add to Cart</button> <span data-field-unless="inStock">Out of Stock</span> </footer> </layout-card> </template></card-list>
Attributes
| Attribute |
Type |
Default |
Description |
src |
URL |
— |
URL to fetch JSON data from. Expects an array or an object with items or data property. |
data-items |
JSON string |
— |
Inline JSON array of items. Takes priority over src. |
data-key |
string |
id |
Property name to use as the unique key for each item. Items whose key is missing or duplicated fall back to their index, so keyless data still renders every item. |
data-layout |
string |
— (block) |
Any global layout value, e.g. grid, stack, reel. Without it the list is display: block. |
Binding Attributes
These go on elements inside the <template> to bind item data.
| Attribute |
Description |
data-field="path" |
Set the element's text content from a property path (e.g. user.name). |
data-field-attr="href: url, title: name" |
Set one or more HTML attributes from property paths. Comma-separated pairs of attribute: path. A null/undefined value removes the attribute; true/false add/remove boolean attributes (aria-* get the literal string). Event handlers and unsafe URLs are refused — see Security. |
data-field-html="path" |
Set innerHTML from a property path. Content is sanitized (scripts, iframes, and event handlers are stripped). |
data-field-if="path" |
Element is shown only if the property value is truthy; otherwise it gets hidden. |
data-field-unless="path" |
Element is shown only if the property value is falsy; otherwise it gets hidden. |
Data Sources
Inline Data
Pass a JSON array directly in the data-items attribute. Good for small, static datasets.
Remote Data
Set src to fetch JSON from a URL. The URL is fetched once when the component upgrades (moving the element in the DOM does not re-fetch) and again whenever src changes; a newer request aborts the older one. The component shows a loading state while fetching and an error state on failure — see States.
12345678
<card-list src="/api/products.json" data-layout="grid" data-key="id"> <template> <layout-card> <header><h3 data-field="name"></h3></header> <section><p data-field="description"></p></section> </layout-card> </template></card-list>
The response can be a plain array, or an object with an items or data property containing the array.
Nested Properties
Access nested data with dot notation. Array indices are also supported: items[0].title.
12345678910111213
<card-list data-layout="stack" data-items='[ {"id": 1, "user": {"name": "Alice", "email": "[email protected]"}}, {"id": 2, "user": {"name": "Bob", "email": "[email protected]"}}]' data-key="id"> <template> <layout-card data-variant="outlined"> <layout-cluster data-layout-justify="between" data-layout-align="center"> <strong data-field="user.name"></strong> <small data-field="user.email"></small> </layout-cluster> </layout-card> </template></card-list>
Attribute Binding
Use data-field-attr to set HTML attributes from item properties. Comma-separated pairs map attributes to property paths. URL attributes only accept safe URLs, so a javascript: value in the data leaves the link without an href.
1234567891011
<card-list data-layout="grid" data-items='[ {"id": 1, "title": "Getting Started", "url": "#start", "desc": "Learn the basics."}, {"id": 2, "title": "API Reference", "url": "#api", "desc": "Full documentation."}]' data-key="id"> <template> <layout-card data-variant="outlined"> <h3><a data-field="title" data-field-attr="href: url, title: desc"></a></h3> <p data-field="desc"></p> </layout-card> </template></card-list>
Conditional Rendering
Use data-field-if and data-field-unless to show or hide elements based on item data. The elements stay in the card and are toggled with the hidden attribute, so when an item's data changes (via .items or data-items) they come back.
123456789101112131415161718
<card-list data-layout="grid" data-items='[ {"id": 1, "name": "Free", "price": "$0/mo", "featured": false}, {"id": 2, "name": "Pro", "price": "$29/mo", "featured": true}]' data-key="id"> <template> <layout-card> <header> <h3 data-field="name"></h3> <layout-badge data-field-if="featured">Popular</layout-badge> </header> <section><p data-field="price"></p></section> <footer> <button data-field-if="featured">Get Started</button> <button data-field-unless="featured">Learn More</button> </footer> </layout-card> </template></card-list>
Rich HTML Content
Use data-field-html to render HTML content from a property. Content is sanitized — <script>, <iframe>, <object>, <embed>, <form>, event handlers, and javascript: URIs are all stripped.
12345678910
<card-list data-items='[ {"id": 1, "title": "Bold", "content": "<strong>Rich</strong> HTML content"}]' data-key="id"> <template> <article> <h3 data-field="title"></h3> <section data-field-html="content"></section> </article> </template></card-list>
Layout
<card-list> has no layout modes of its own: it uses the global data-layout attribute like any other container, including its modifiers.
12345678
<card-list data-layout="grid" data-layout-min="l" data-layout-gap="xl" src="/api/products.json"> <template>...</template></card-list> <card-list data-layout="reel" data-items='[...]' role="region" aria-label="Featured products" tabindex="0"> <template>...</template></card-list>
With data-layout="reel" the cards scroll horizontally (see <layout-reel>). When the cards hold no links or buttons, add role="region", an aria-label and tabindex="0" so keyboard users can scroll the strip and screen readers can name it.
States
While a src fetch is in flight the list has data-loading and aria-busy="true". Loading and error text is written into a <p role="status" data-card-list-status> child the component creates, so screen readers hear it; it is visually hidden while empty. The error banner uses the theme's --color-error-* tokens. data-error is cleared as soon as a new fetch starts. A failed fetch leaves any existing cards (or [data-fallback] content) in place.
| Attribute |
When |
Visual |
data-loading |
During src fetch |
Reduced opacity, aria-busy="true", "Loading…" status text. |
data-error |
Fetch failure |
Error banner (status text) with the message, e.g. HTTP 404. |
Events
| Event |
Detail |
Description |
card-list:rendered |
{ count: number } |
Fired after items are rendered. Bubbles. |
card-list:items-changed |
{ items, source } |
Fired when the items change. source is 'attribute' (from data-items, src or data-key) or 'api' (the .items setter), so frameworks can ignore echoes of their own assignments. Bubbles. |
card-list:upgraded |
— |
Fired once the component has found its template. Bubbles. |
12345
const list = document.querySelector('card-list'); list.addEventListener('card-list:rendered', (e) => { console.log(`Rendered ${e.detail.count} items`);});
JavaScript API
| Method / Property |
Description |
.items |
Get the current items array, or assign a new array. Assignment runs a keyed diff: cards whose key persists are kept (focus and scroll survive) and re-bound with the new data. Assigning cancels an in-flight src fetch. Items assigned before the element is connected render on upgrade. |
1234567891011
const list = document.querySelector('card-list'); list.items = [ { id: 1, name: 'Item A' }, { id: 2, name: 'Item B' },]; list.addEventListener('card-list:items-changed', (e) => { if (e.detail.source === 'api') return; store.set(e.detail.items);});
Security
All data-field paths are validated against a strict regex. Only simple property access is allowed:
- Allowed:
name, user.email, items[0].title
- Rejected:
price.toFixed(2), `${price}`, price * 1.1, stock > 0 ? 'yes' : 'no'
Invalid paths log a console warning and resolve to empty content.
data-field-html passes the value through the shared HTML sanitizer. data-field-attr is guarded too:
- Event-handler attributes (
on*), style and srcdoc are never written from data.
- URL attributes (
href, src, action, formaction, xlink:href, poster, cite, ping, data, srcset, …) only accept http:, https:, mailto:, tel:, relative URLs and fragments. Anything else (javascript:, data:, vbscript:, …) is refused with a console warning and the attribute is removed.
Progressive Enhancement
Without JavaScript, the <template> renders nothing. Put static content for no-JS readers in a child marked data-fallback: the component removes it on the first successful render (it stays if the fetch fails).
123456789
<card-list src="/api/team.json" data-layout="grid"> <ul data-fallback> <li><a href="/team/alice">Alice Johnson</a></li> <li><a href="/team/bob">Bob Martinez</a></li> </ul> <template> <layout-card><h3><a data-field="name" data-field-attr="href: url"></a></h3></layout-card> </template></card-list>