Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Twig helpers for stimulus vs. "native" syntax #17

Open
ThomasLandauer opened this issue Sep 27, 2022 · 3 comments
Open

Twig helpers for stimulus vs. "native" syntax #17

ThomasLandauer opened this issue Sep 27, 2022 · 3 comments

Comments

@ThomasLandauer
Copy link
Contributor

On https://symfonycasts.com/screencast/stimulus/ajax-html you're using "native" HTML/Stimulus syntax

data-search-preview-target=""

whereas on previous pages, you're using the Twig helpers, e.g.

{{ stimulus_controller('search-preview') }}

I have a general question: What's the advantage of those Twig helpers at all? At symfony/webpack-encore-bundle#119 (comment), you're saying:

Remembering the syntax for targets and actions is not always easy :)

But the Twig syntax needs to be remembered too... ;-)

If there are true advantages, they should be mentioned at https://symfony.com/doc/current/frontend/encore/simple-example.html#stimulus-symfony-ux - and if there aren't, then both syntaxes should be shown (as equivalently "valid" alternatives).

@bocharsky-bw
Copy link
Member

But the Twig syntax needs to be remembered too

Well, you're right :) But if you're using PhpStorm with Symfony plugin installed and configured - it will help you with autocomplition. And so, it's easier to remember Twig syntax IMO. But it might be subjective, agree. You can use whatever way you like the most (or you can remember better), that's not something super important. If you want to learn more (see more useful examples) so you could compare these 2 ways - I'd recommend you to take a look at the docs: https://github.com/symfony/webpack-encore-bundle#stimulus--symfony-ux-helper

Cheers!

@weaverryan
Copy link
Contributor

stimulus_controller() is not very useful until you want to pass values into your controller - e.g.

{{ stimulus_controller('search-preview', {
    withFoo: 'bar'
}) }}

Then it's GREAT because it handles also adding the value attributes, which I honestly can't remember :). So that is a major advantage. And so, for consistency, it makes sense to always recommend using it. Even if you don't have any values initially, you're setup and ready to add them easily later.

@ThomasLandauer
Copy link
Contributor Author

It's:

<div data-controller="search" data-search-url-value="...">

...which looks reasonable to me - it follows Stimulus' logic.

Consistency: Never using the Twig helpers is equally consistent as always using them ;-)

Anyway, when I come up with a PR for https://github.com/symfony/webpack-encore-bundle#stimulus--symfony-ux-helper, presenting both ways as equal alternatives - will you merge it?
The most important point IMO is that Twig is just syntax sugar. Right now, it looks like it's mandatory. And one point against using Twig is that it's harder to understand the official Stimulus docs: https://stimulus.hotwired.dev/reference/controllers

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

No branches or pull requests

3 participants