Symfony 2:image paths in javascript file with assetic -
i'm trying make path image js file in symfony2 project using asset this:
var arr = $('<img src="{{ asset("assets/images/linkarrow.png")}}">').css({ position: 'absolute', ... });
but i'm having following error
"networkerror: 404 not found - mywebsite.com/bundles/web/app_dev.php/project/1/assets /images/linkarrow.png"
my image under web/assets file.
you cannot use twig syntax in javascript file. if can put script in template file work then. clean way define assets base path variable javascript. example, solve problem can add following code in layout template:
<script> var assetsbasedir = "{{ asset('assets/') }}" </script>
nb: make sure define script before loading javascript file, if referencing variable directly in js file
then can use in javascript files like:
var arr = $('<img src="' + assetsbasedir + 'images/linkarrow.png'+ '">').css({ position: 'absolute', ... });
happy coding!
Comments
Post a Comment