As explained in the official Symfony2 docs, in a Twig template one can link to an asset as follows:
<img src="{{ asset('images/logo.png') }}" alt="My logo" />
which will render the right path to the logo.png resource (it depends on the folder where the Symfony2 app lives).
Well, suppose to use a Javascript code that has to load an image at runtime (e.g. as a result from an autocomplete), how can one achieve in Javascript the same result provided by asset function in Twig?
Some note:
- Maybe exists something like the FOSJsRoutingBundle, but I really ignore it!
- A dirty way could be to generate the path for a known resource, e.g. the
logo.pngfile<script>var known = {{asset('images/logo.png)')}}</script>and so retrieve the path by a string replace on variableknownby replacing stringimages/logo.pngwith an empty one. But it is a dirty way!