Accessing Data in Templates

Display collection items in your component templates

Collection data is automatically available in your templates through the collections object. Each collection is accessible by its property name as defined in your component's properties.json.

Syntax

Access a collection using collections.{property-name}:

@each(item in collections.features)
    <div>{{item.title}}</div>
@endeach

Basic Example

Given a features collection with title and description properties:

<ul class="features-list">
    @each(feature in collections.features)
        <li>
            <h3>{{feature.title}}</h3>
            <p>{{feature.description}}</p>
        </li>
    @endeach
</ul>

Real-World Examples

Audio Playlist Tracks

The Audio Playlist component iterates over tracks to build a playlist:

Note: In this example, tracks is passed from hooks.js after being extracted from rw.collections.tracks.

Feature Cards with Icons

Display a grid of feature cards:

Pricing Tiers

Create a pricing table from collection data:

Team Members

Display team member profiles:

Using Loop Variables

Within collection loops, you have access to special loop variables:

Variable
Description

::index

Zero-based index of the current item

::isFirst

True if this is the first item

::isLast

True if this is the last item

Example with Loop Variables

First/Last Styling

Handling Empty Collections

Check if a collection has items before rendering:

The hasFeatures boolean is typically computed in your hooks.js file. See Data Collections in hooks.js for details.

Accessing Nested Properties

Collection items can have complex nested data:

Best Practices

  1. Use meaningful loop variable names - Choose names that describe the item: feature, track, member, not generic names like item or i.

  2. Handle empty states - Always consider what happens when a collection is empty.

  3. Use hooks.js for preprocessing - If you need to transform collection data, do it in hooks.js rather than complex template logic.

  4. Access via passed props when needed - For complex components, pass collections from hooks.js to templates as props for cleaner code.

Last updated

Was this helpful?