How to use assetic to render an (unoptimised) image tag in Symfony2 + Twig

jlb picture jlb · Dec 6, 2012 · Viewed 7.5k times · Source

Is there a shorter way to have Symfony2 + Assetic + Twig generate image urls than:

{% image '@ACMEBundle/Resources/public/img/longer.png' %}
  <img src="{{ asset_url }}" />
{% endimage %}

Ideally, something like:

<img src="{{ bundle_resource_url('@ACMEBundle/Resources/public/img/shorter.png') }}" />

Answer

lifo picture lifo · Dec 7, 2012

If your assets are in your web directory then use this instead:

<img src="{{ asset('bundles/acme/img/shorter.jpg') }}" />

You can have Assetic install your assets for you by running this:

app/console assets:install path/to/web

Where path/to/web is your webroot.