Symfony2 Path to image in twig template Symfony2 Path to image in twig template symfony symfony

Symfony2 Path to image in twig template


Please try this

<img src="{{ asset('bundles/acmedemo/img/weather_icons/blizzard.gif') }}" />

You should istall your assets into web directory with the command

app/console assets:install web


You can use an image of a bundle:

{% image '@CoreBundle/Resources/public/images/your-image.jpg'  output="images/your-image.jpg" %}<img src="{{ asset_url }}" width="100%" height="100%" alt="my alt of image" class="pull-left">{% endimage %}


Assetic solution: You will get better performance with assetic as opposed to asset.

directory structure for example:

C:\xampp\htdocs\yourproject\src\AppBundle\Resources\public\images\yourimage.png

project structure for example:

yourproject\src\AppBundle\Resources\public\images\yourimage.png

in yourhtml.html.twig call:

{% image '@AppBundle/Resources/public/images/yourimage.png' %}                    <img src="{{ asset_url }}" alt="Example" />                {% endimage %}

NOTE:

This is providing you install assetic bundle which can be set in composer.json of the project as such:

"require": {"php": ">=5.3.3","sensio/framework-extra-bundle": "~3.0","symfony/assetic-bundle": "~2.6", },

google for more instruction on installing assetic bundle symfony2.

that is it.

source:

http://symfony.com/doc/2.7/cookbook/assetic/asset_management.html#cookbook-assetic-cssrewrite