簡體   English   中英

Twig&symfony,在{%javascript%}標簽中使用變量

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

我有這個非常樣本的樹枝模板:

<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>

我想在{%javascript%}中添加{{file_js1}}和{{file_js2}},我該怎么做? (用於動態管理JavaScript文件)。 我做了幾次嘗試,但是沒有用twig找到好的語法。

您是要包含文件還是動態包含文件? 如果您只是想包括它們,請確保它們在您的捆綁包中並使用:

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

看來您的問題與資產問題更為相關,但是如果您真正想要的是添加變量以使其在樹枝中可用,則需要執行以下操作:

twig:
    globals:
        my_parameter: '%whatever_parameter%'

然后,您可以在樹枝模板中執行以下操作:

{{ my_parameter }}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM