简体   繁体   English

Symfony 5.2 - 在 Twig 中渲染刺激 controller

[英]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"当我以这种方式在 Twig 中添加刺激组件时: {{ stimulus_controller('test')}} ,它呈现文本: data-controller="test"

When I use <div data-controller='test'></div> , the controllers renders perfectly.当我使用<div data-controller='test'></div>时,控制器会完美呈现。

Is there a tuning setup to make to activte this twig extension?是否有调整设置来激活这个 twig 扩展? I didn't find anything this way, and the extension is well recognized by my IDE which place me on the extension declaration....我没有以这种方式找到任何东西,并且我的 IDE 很好地识别了扩展名,这将我置于扩展名声明中......

Thanks for your help谢谢你的帮助

Just use it like this:像这样使用它:

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

This will output:这将 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通过查看vendor ,您可以在 Twig 扩展中找到stimulus_controller控制器的声明: Symfony\WebpackEncoreBundle\Twig\StimulusTwigExtension

The aim of this extension is to render the data-controller attribute and, optionnaly, some data-[controller-identifier]-value attributes.此扩展的目的是呈现data-controller属性和可选的一些data-[controller-identifier]-value属性。

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.)它不会呈现div因为您的 Stimulus controller 可以附加到页面中的任何类型的节点( spanbody等)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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