Symfony 2: How to create image paths in javascript file?

I have been looking for this for a long time! I need to show images on some cursors and mice. The code is written in the js file. But the image path must be generated. Is there a way to generate image paths using something like this

{{asset ('bundles / mybundle / images / menu_down.png')}}

Can FOSJsRoutingBundle be used to create image paths in js files?

+4
source share
3 answers

Agree with Nick's answer, but he forgot the quotation marks around {{asset (...)}}.

So this should be written like this:

var menuDownUrl = "{{ asset('bundles/mybundle/images/menu_down.png') }}"; 

if you want window.menuDownUrl work and not window.menuDownUrl you "undefined"

+6
source

You can set JS global variables on your actual page:

 <script> var menuDownUrl = "{{ asset('bundles/mybundle/images/menu_down.png') }}"; </script> 

And then set inside your javascript file to call the global variable: window.menuDownUrl

It then creates a dependency inside your javascript file, but allows you to dynamically set this image.

+13
source

You can put the input hidden and set the path to the image.

 <input type="hidden" id="img_path" value="{{ asset('images/circle_loading.gif') }}"> 

After that in jQuery you can use it as follows

 var img = "<img src=\'"+ $("#img_path").val() +"\'>"; $("div").html(img); 
+8
source

Source: https://habr.com/ru/post/1401571/


All Articles