title: Symfony and assets usage at runtime with Javascript
body: <p>As explained in the official <a href="http://symfony.com/doc/current/book/templating.html#linking-to-assets" rel="nofollow">Symfony2 docs</a>, in a Twig template one can link to an asset as follows:</p>
  
  <pre><code>&lt;img src="{{ asset('images/logo.png') }}" alt="My logo" /&gt;
  </code></pre>
  
  <p>which will render the right path to the <code>logo.png</code> resource (it depends on the folder where the Symfony2 app lives).</p>
  
  <p>Well, suppose to use a Javascript code that has to load an image at runtime (e.g. as a result from an autocomplete), <strong>how can one achieve in Javascript the same result provided by <code>asset</code> function in Twig</strong>?</p>
  
  <p>Some note:</p>
  
  <ol>
  <li>Maybe exists something like the <a href="https://github.com/FriendsOfSymfony/FOSJsRoutingBundle" rel="nofollow">FOSJsRoutingBundle</a>, but I really ignore it!</li>
  <li>A dirty way could be to generate the path for a known resource, e.g. the <code>logo.png</code> file <code>&lt;script&gt;var known = {{asset('images/logo.png)')}}&lt;/script&gt;</code> and so retrieve the path by a string replace on variable <code>known</code> by replacing string <code>images/logo.png</code> with an empty one. But it is a dirty way!</li>
  </ol>