Variable inside an asset declaration in Twig

My inline style is as follows:

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

part of the id url should vary depending on the varibale. How can I do this inside an asset.

I tried:

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

But to no avail.

+8
html php symfony twig
source share
2 answers

Use ~ to concatenate ,

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

Besides,

You do not need to insert delimiters {{ ... }} . The ones you used to carry over the asset() call are also used to print any other variable that they contain.

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

All Articles