Skip to content

Commit 07a53b6

Browse files
committed
Add wp-each documentation
1 parent 4643f4b commit 07a53b6

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

packages/interactivity/docs/2-api-reference.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,78 @@ But it can also be used on other elements:
552552

553553
When the list is re-rendered, the Interactivity API will match elements by their keys to determine if an item was added/removed/reordered. Elements without keys might be recreated unnecessarily.
554554

555+
556+
#### `wp-each`
557+
558+
The `wp-each` directive is intended to render a list of elements. The directive can be used in `<template>` tags, being the value a path to an array stored in the global state or the context. The content inside the `<template>` tag is the template used to render each of the items.
559+
560+
Each item is included in the context under the `item` name by default, so directives inside the template can access the current item.
561+
562+
For example, let's consider the following HTML.
563+
564+
```html
565+
<ul data-wp-context='{ "list": [ "hello", "hola", "olá" ] }'>
566+
<template data-wp-each="context.list" >
567+
<li data-wp-text="context.item"></li>
568+
</template>
569+
</ul>
570+
```
571+
572+
It would generate the following output:
573+
574+
```html
575+
<ul data-wp-context='{ "list": [ "hello", "hola", "olá" ] }'>
576+
<li data-wp-text="context.item">hello</li>
577+
<li data-wp-text="context.item">hola</li>
578+
<li data-wp-text="context.item">olá</li>
579+
</ul>
580+
```
581+
582+
The prop that holds the item in the context can be changed by passing a suffix to the directive name. In the following example, we change the default prop `item` to `greeting`.
583+
584+
```html
585+
<ul data-wp-context='{ "list": [ "hello", "hola", "olá" ] }'>
586+
<template data-wp-each--greeting="context.list" >
587+
<li data-wp-text="context.greeting"></li>
588+
</template>
589+
</ul>
590+
```
591+
592+
By default, it uses each element as the key for the rendered nodes, but you can also specify a path to retrieve the key if necessary, e.g., when the list contains objects.
593+
594+
For that, you must use `data-wp-each-key` in the `<template>` tag and not `data-wp-key` inside the template content. This is because `data-wp-each` creates
595+
a context provider wrapper around each rendered item, and those wrappers are the ones that need the `key` property.
596+
597+
```html
598+
<ul data-wp-context='{
599+
"list": [
600+
{ "id": "en", "value": "hello" },
601+
{ "id": "es", "value": "hola" },
602+
{ "id": "pt", "value": "olá" }
603+
]
604+
}'>
605+
<template
606+
data-wp-each--greeting="context.list"
607+
data-wp-each-key="context.greeting.id"
608+
>
609+
<li data-wp-text="context.greeting.value"></li>
610+
</template>
611+
</ul>
612+
```
613+
614+
For server-side rendered lists, another directive called `data-wp-each-child` ensures hydration works as expected. It should be placed in the server-side rendered elements.
615+
616+
```html
617+
<ul data-wp-context='{ "list": [ "hello", "hola", "olá" ] }'>
618+
<template data-wp-each--greeting="context.list" >
619+
<li data-wp-text="context.greeting"></li>
620+
</template>
621+
<li data-wp-each-child>hello</li>
622+
<li data-wp-each-child>hola</li>
623+
<li data-wp-each-child>olá</li>
624+
</ul>
625+
```
626+
555627
### Values of directives are references to store properties
556628

557629
The value assigned to a directive is a string pointing to a specific state, action, or side effect.

0 commit comments

Comments
 (0)