简体   繁体   中英

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 ? (for manage dynamically javascript file include). I do several tries but I didn't find the good syntax with 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 }}

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