Niroshan Ranapathi
Niroshan Ranapathi

Reputation: 3047

Dynamically Change image source destination Twig

I want to change user profile image for different user. I used following code for add image source

 <img src="{{ asset('bundles/dist/img/user4-128x128.jpg') }}" alt="User Image" class="img-circle" />

All images are saved as username in bundles/dist/img/ directory

i try change image source following manner

<img src="{{ asset('bundles/dist/img/ '~{{ app.user.username }}~'.jpg') }}" alt="User Image" class="img-circle" />

But it is not working ? What is the best way doing this ?

Upvotes: 1

Views: 783

Answers (1)

Nickolaus
Nickolaus

Reputation: 4835

You are nearly right, what should work is this:

<img src="{{ asset('bundles/dist/img/'~ app.user.username ~'.jpg') }}" alt="User Image" class="img-circle" />

you need to remove the brackets inside, because you already opened them before calling the asset function therefore the variable is already available in this context

Upvotes: 2

Related Questions