简体   繁体   中英

Symfony 5.2 - render Stimulus controller in Twig

I think this is a simpe issue, but I don't understand why it happens:

When I add a stimulus component in Twig this way: {{ stimulus_controller('test')}} , it renders the text: data-controller="test"

When I use <div data-controller='test'></div> , the controllers renders perfectly.

Is there a tuning setup to make to activte this twig extension? I didn't find anything this way, and the extension is well recognized by my IDE which place me on the extension declaration....

Thanks for your help

Just use it like this:

<div {{ stimulus_controller('test') }}></div>

This will output:

<div data-controller="test"></div>

By looking into the vendor , you can find the declaration of stimulus_controller inside a Twig extension: Symfony\WebpackEncoreBundle\Twig\StimulusTwigExtension

The aim of this extension is to render the data-controller attribute and, optionnaly, some data-[controller-identifier]-value attributes.

It will not render a div because your Stimulus controller can be attached to any kind of node in your page (a span , the body , etc.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM