Skip to content

Conversation

@Kocal
Copy link
Member

@Kocal Kocal commented Apr 27, 2021

Hi 👋

This PR is a proposal for #119, which adds stimulus_action() and stimulus_target() Twig functions.

stimulus_action() can be a bit hard to use because it supports many syntaxes, since Stimulus allows to use multiple actions in a single data-action attribute and we should takes care of the default event:

  • stimulus_action('controller', 'method')
  • a custom event: stimulus_action('controller', 'method', 'event)
  • multiple controllers/actions:
{{ stimulus_action({
  'controller': 'method',
  'controller-2': ['method', 'method2'],
  'controller-3': {'click': 'onClick'},
  'controller-4': ['method', {'click': 'onClick'}, {'window@resize': 'onWindowResize'}],
}) }}

For stimulus_target:

  • stimulus_target('controller', 'target')
  • stimulus_target('controller', 'target1 target2'), Stimulus allows multiple targets
  • stimulus_target({ 'controller1': 'target', 'controller2': 'target2 target3' }), array syntax

Usage:

<div {{ stimulus_controller('foo') }}>
  <div {{ stimulus_target('foo', 'myTarget') }}></div>
  <!-- <div data-foo-target="myTarget"></div> -->

  <div {{ stimulus_target('foo', 'myTarget mySecondTarget') }}></div>
  <!-- <div data-foo-target="myTarget mySecondTarget"></div> -->

  <div {{ stimulus_target({ 'foo': 'myTarget', 'bar': 'anotherTarget' }) }}></div>
  <!-- <div data-foo-target="myTarget" data-bar-target="anotherTarget"></div> -->

  {# Listen to default event and call "onClick" #}
  <a {{ stimulus_action('foo', 'onClick') }}>A link</a>
  <!-- <div data-action="foo#onClick">A link</a> -->

  {# Listen to event "click" and call "onClick" #}
  <a {{ stimulus_action('foo', 'onClick', 'click') }}>A Link</a>
  <!-- <div data-action="click->foo#onClick">A link</a> -->

  {# Listen to default event, call "onClick" from "foo" controller and "onClick" + "onSomethingElse" from "bar" controller #}
  <a {{ stimulus_action({
    'foo': 'onClick', 
    'bar': ['onClick', 'onSomethingElse']
  }) }}>A link</a>
  <!-- <div data-action="click->foo#onClick bar#onClick bar#onSomethingElse">A div</div> -->

  {# It is possible to pass a map, here "bar#onWindowResize" will be called on "window@resize" #}
  <div {{ stimulus_action({
    'foo': 'onClick', 
    'bar': ['onClick', {'window@resize': 'onWindowResize'}]
  }) }}>A div</a>
  <!-- <div data-action="foo#onClick bar#onClick window@resize->bar#onWindowResize">A div</div> -->
</div>

WDYT? Thanks!


For #119 (comment), I think it should belongs to another PR.

@Kocal Kocal changed the title feat(twig): implements stimulus_action() and stimulus_target() Twig functions, close #119 WIP feat(twig): implements stimulus_action() and stimulus_target() Twig functions, close #119 Apr 27, 2021
@Kocal Kocal changed the title WIP feat(twig): implements stimulus_action() and stimulus_target() Twig functions, close #119 feat(twig): implements stimulus_action() and stimulus_target() Twig functions, close #119 Apr 27, 2021
Copy link

@moismailzai moismailzai left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me, just a few thoughts!

Copy link

@moismailzai moismailzai left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall the proposed syntax / documentation / implementation looks good to me. Seeing some duplicated logic in the methods but I think there may be some more work done in here (eg. #123) so maybe not a good idea to start consolidating logic yet. +1 from me.

Copy link
Member

@weaverryan weaverryan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this a lot! The super advanced syntaxes are ugly, but logical - and they will be rarely used. The simple syntax is... nice and simple :). Good work!

* @return string
* @throws \Twig\Error\RuntimeError
*/
public function renderStimulusAction(Environment $env, $dataOrControllerName, string $actionName = null, string $eventName = null): string
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't forget to expose these new functions up in getFunctions() ;)

Copy link
Member Author

@Kocal Kocal May 6, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OMG yes 😱

I totally forgot about them, usually I write tests for Twig Extensions by creating a fake loader and doing assertions on $twig->render() This way I can test the extension and its integration with Twig.

Thanks! 😄

@Kocal Kocal requested a review from weaverryan May 6, 2021 18:35
@Kocal
Copy link
Member Author

Kocal commented May 18, 2021

Friendly ping @weaverryan @tgalopin

Thanks!

@weaverryan weaverryan force-pushed the feat/stimulus-target-and-action branch from 7806114 to c022b08 Compare May 19, 2021 18:11
@weaverryan
Copy link
Member

As I don't see any other feedback, I'm going to merge this. Thanks @Kocal! But... would you mind opening a PR to the README to document these? I'd like to have that before we release :)

@weaverryan weaverryan merged commit 16d6f67 into symfony:main May 19, 2021
@Kocal Kocal deleted the feat/stimulus-target-and-action branch May 19, 2021 18:24
@Kocal
Copy link
Member Author

Kocal commented May 19, 2021

Thanks @weaverryan, I'm on it!

Kocal added a commit to Kocal/webpack-encore-bundle that referenced this pull request May 19, 2021
Kocal added a commit to Kocal/webpack-encore-bundle that referenced this pull request May 19, 2021
@Kocal
Copy link
Member Author

Kocal commented May 19, 2021

Opened at #125

weaverryan added a commit that referenced this pull request Jun 6, 2021
This PR was merged into the main branch.

Discussion
----------

doc: stimulus_action() and stimulus_target()

Following #124 and #124 (comment)

Rendered: https://github.com/Kocal/webpack-encore-bundle/tree/doc/stimulus-target-and-actions#stimulus_action

Commits
-------

1fa2503 doc: stimulus_action() and stimulus_target()
Keemosty12 added a commit to Keemosty12/webpack-encore-bundle that referenced this pull request Jun 12, 2025
Keemosty12 added a commit to Keemosty12/webpack-encore-bundle that referenced this pull request Jun 12, 2025
This PR was merged into the main branch.

Discussion
----------

doc: stimulus_action() and stimulus_target()

Following #124 and symfony/webpack-encore-bundle#124 (comment)

Rendered: https://github.com/Kocal/webpack-encore-bundle/tree/doc/stimulus-target-and-actions#stimulus_action

Commits
-------

1fa2503 doc: stimulus_action() and stimulus_target()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants