Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A variable inside an asset declaration in Twig

My inline style looks like:

style="background-image: url({{ asset('bundles/testblog/images/id.jpg') }});"

the part id of the url must change depending on a varibale. How can I make this happen inside the asset.

I tried :

style="background-image: url({{ asset('bundles/testblog/images/'{{variable}}'.jpg') }});" 

But to no avail.

like image 390
Adib Aroui Avatar asked Oct 01 '13 11:10

Adib Aroui


2 Answers

Use ~ for concatenation,

style="background-image: url({{ asset('bundles/testblog/images/' ~ variable ~ '.jpg') }});"

Also,

You don’t need to nest {{ ... }} delimiters. The ones you used to wrap asset() call are also used to print any other variable they contain.

like image 63
Ahmed Siouani Avatar answered Sep 24 '22 23:09

Ahmed Siouani


style="background-image: url({{ asset('bundles/testblog/images/' ~ variable ~ '.jpg') }});" 
like image 38
nni6 Avatar answered Sep 22 '22 23:09

nni6