简体   繁体   English

Twig&symfony,在{%javascript%}标签中使用变量

[英]Twig & symfony, use a variable in a {% javascript %} tag

I have this very sample twig template : 我有这个非常样本的树枝模板:

<html>
    <head>
        <meta charset="UTF-8"/>
        <title>my title</title>
    </head>
    <body>
        {% javascripts '@jquery' '@bootstrap_js' output='javascript.js' %}
            <script src="{{ asset_url }}"></script>
        {% endjavascripts %}
        <p>its the name of the first js file : {{ file_js1 }}</p> {# print '@javascript1label_defined_from_asseticconfig' #}
        <p>its the name of the second js file : {{ file_js2 }}</p> {# print '@javascript2label_defined_from_asseticconfig' #}
    </body>
</html>

I want add the {{ file_js1 }} and the {{ file_js2 }} in the {% javascript %} , how can I do that ? 我想在{%javascript%}中添加{{file_js1}}和{{file_js2}},我该怎么做? (for manage dynamically javascript file include). (用于动态管理JavaScript文件)。 I do several tries but I didn't find the good syntax with twig.. 我做了几次尝试,但是没有用twig找到好的语法。

Are you just trying to include the files, or dynamically include them? 您是要包含文件还是动态包含文件? If you are just trying to include them, make sure they are in your bundle and use: 如果您只是想包括它们,请确保它们在您的捆绑包中并使用:

<script src="{{ asset('path/name/goes/here.js') }}"></script>

It seems that your problem is more related to assetic, but if what you really want is to add a variable to have it available in twig you need to do: 看来您的问题与资产问题更为相关,但是如果您真正想要的是添加变量以使其在树枝中可用,则需要执行以下操作:

twig:
    globals:
        my_parameter: '%whatever_parameter%'

And then you can do in your twig template: 然后,您可以在树枝模板中执行以下操作:

{{ my_parameter }}

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

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