Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 Path to image in twig template

Tags:

I store img in Acme/DemoBundle/Resources/public/img/weather_icon/blizzard.gif I want to put this img in my template so I did

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

and

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

and

And this did't work. I did assets:install and assetic:dump

Solved it worked

<img src="{{ asset('img/weather_icons/Blizzard.gif') }}" alt="Symfony!" /> 
like image 560
konadrian Avatar asked May 06 '13 11:05

konadrian


People also ask

How do I display an image in Twig template?

<img src="{{ asset('../../public/images/Tux. png') }}"> <img src="{{ asset('images/basketball. jpg') }}" alt="first"/> <img style="display: block;" src="{{ asset('images/Tux. png') }}" alt="...">

How do I get the current URL in twig?

You can get the current URL in Twig/Silex 2 like this: global. request. attributes. get('_route') .

How do you set a variable in Twig template?

This can be accomplished by creating an array of the entry years. Inside the loop that's building the array, a conditional checks if the year already exists in the array... if the year exists, a variable is set to TRUE which you can use in your conditional later down on the page. Save this answer.

Can you upload pictures to twig?

Example below lets you upload files to your_project/Resources/private/uploads/images folder and serve them in twig. When it comes to serving the images, it is a bit different to classic way. You create a route to return you image as BinaryFileResponse then show it in your twig.


1 Answers

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 
like image 117
Nisam Avatar answered Sep 22 '22 16:09

Nisam