Slim.aouadi
Slim.aouadi

Reputation: 53

Display image in twig symfony from database

I am trying to display an image from my database into a twig file but it doesn't work, the image is not displayed. I don't know where is the problem.

The image name is stored as a string in the database.

<tr class="">
    <td>{{ boutique.getId() }}</td>
    <td>{{ boutique.getNomBoutique() }}</td>
    <td>{{ boutique.getNomResponsableBoutique() }}</td>
    <td>{{ boutique.getAdresse() }}</td>
    <td>{{ boutique.getContact() }}</td>
    <td>{{ boutique.getFournisseur() }}</td>
    <td>{{ boutique.getType() }}</td>
    <td><img src="{{ asset('public/images/{{ boutique.getImage()}}') }}" class="img-rounded"/></td>
    <td><a href="#" class="btn btn-outline-success btn-sm">View Order</a></td>
    <td><a href="#" class="btn btn-outline-warning btn-sm">Cancel</a></td>
</tr>

Upvotes: 1

Views: 9073

Answers (2)

Khaled Boussoffara
Khaled Boussoffara

Reputation: 1771

<img class="img-thumbnail" src="{{ asset('uploads/avatars/' ~ user.avatar) }}" alt="user avatar">

symfony 5 webpack encore, my upload folder is in : \public\uploads\avatars

Upvotes: 1

Alan T.
Alan T.

Reputation: 1430

You wrote {{ asset('public/images/{{ boutique.getImage()}}') }}. This won't work, you need to properly concatenate your strings like this:

{{ asset('public/images/' ~ boutique.getImage()) }}

You can also use string interpolation within double-quoted string if you want:

{{ asset("public/images/#{boutique.getImage()}") }}

Upvotes: 6

Related Questions