Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
doc: stimulus_action() and stimulus_target()
Following #124 and #124 (comment)
  • Loading branch information
Kocal committed May 19, 2021
commit 1fa250362f44c0f7115959a2a5fe399c272b0bbb
64 changes: 63 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,9 @@ class ScriptNonceSubscriber implements EventSubscriberInterface
}
```

## Stimulus / Symfony UX Helper: stimulus_controller
## Stimulus / Symfony UX Helper

### stimulus_controller

This bundle also ships with a special `stimulus_controller()` Twig function
that can be used to render [Stimulus Controllers & Values](https://stimulus.hotwire.dev/reference/values).
Expand Down Expand Up @@ -238,4 +240,64 @@ associative array in the first argument:
</div>
```

### stimulus_action

The `stimulus_action()` Twig function can be used to render [Stimulus Actions](https://stimulus.hotwire.dev/reference/actions).

For example:
```twig
<div {{ stimulus_action('controller', 'method') }}>Hello</div>
<div {{ stimulus_action('controller', 'method', 'click') }}>Hello</div>

<!-- would render -->
<div data-action="controller#method">Hello</div>
<div data-action="click->controller#method">Hello</div>
```

If you have multiple actions and/or methods on the same element, pass them all as an
associative array in the first argument:
```twig
<div {{ stimulus_action({
'controller': 'method',
'other-controller': ['method', {'window@resize': 'onWindowResize'}]
}) }}>
Hello
</div>

<!-- would render -->
<div data-action="controller#method other-controller#method window@resize->other-controller#onWindowResize">
Hello
</div>
```

### stimulus_target

The `stimulus_target()` Twig function can be used to render [Stimulus Targets](https://stimulus.hotwire.dev/reference/targets).

For example:
```twig
<div {{ stimulus_target('controller', 'a-target') }}>Hello</div>
<div {{ stimulus_target('controller', 'a-target second-target') }}>Hello</div>

<!-- would render -->
<div data-controller-target="a-target">Hello</div>
<div data-controller-target="a-target second-target">Hello</div>
```

If you have multiple targets on the same element, pass them all as an
associative array in the first argument:
```twig
<div {{ stimulus_target({
'controller': 'a-target',
'other-controller': 'another-target'
}) }}>
Hello
</div>

<!-- would render -->
<div data-controller-target="a-target" data-other-controller-target="another-target">
Hello
</div>
```

Ok, have fun!