CoderTn
CoderTn

Reputation: 997

Display images in twig using asset in Symfony 3

i have a list of objects which has an image as an attribute(name of the image as a string) , i am looping this list in twig and i try to display the image in an img html tag which takes it's source of image through a symfony 'asset' path . this the twig part of code :

{% for  offre in listeoffres  %}
    <li>   
        <img src="{{asset('images/')}} {{ offre.nomImage }}" alt="">
    </li>
    <li>
        {{ offre.titre }}  
    </li>  
{% endfor %}

Notes :

Upvotes: 0

Views: 1993

Answers (2)

Edi Modrić
Edi Modrić

Reputation: 2310

While your solution works, Tthe correct way to use the asset function would be to pass the full path of the image to the function call:

{{ asset('images/' ~ offre.nomImage) }}

Upvotes: 1

CoderTn
CoderTn

Reputation: 997

I have solved it. The problem was the space between the asset and the image name.
So the image source should have been like this :

{{asset('images/')}}{{ offre.nomImage}}

Upvotes: 0

Related Questions