[英]TWIG - Include different JS asset file depending to a variable
I would like to load different JavaScript files in a Twig template depending to a variable setup before.我想根据之前的变量设置在 Twig 模板中加载不同的 JavaScript 文件。
And i try to do something like that :我尝试做这样的事情:
{% set myFileToLoad = 'awesomeScript.js' %}
{% javascripts
'js/forms/' ~ myFileToLoad
%}
<script src="{{ asset_url }}"></script>
{% endjavascripts %}
But I got this error each time I try to edit the name of the asset :但是每次尝试编辑资产名称时都会出现此错误:
Attempted to call an undefined method named "getFilename" of class "Twig\\TokenStream".
试图调用类“Twig\\TokenStream”的名为“getFilename”的未定义方法。
Is there a way to do it better ?有没有办法做得更好? Thank you in advance
先感谢您
You seem to be using an old syntax based on the assetic bundle that is no longer supported on Symfony >= 4.0, but you can use standard blocks instead, and are pretty close to getting it right:您似乎正在使用基于 Symfony >= 4.0 不再支持的资产包的旧语法,但您可以改用标准块,并且非常接近于正确使用:
{% set myFileToLoad = 'awesomeScript.js' %}
{% block javascripts %}
<script src="{{ asset('js/forms/' ~ myFileToLoad) }}"></script>
{% endblock %}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.