Question

I've just recently found out from posts on SO that it isn't possible to render dynamic images in twig using assetic. So i've gone about trying to add the dynamic image in using the asset command.

Currently, i have tried the following:

{% for item in test %}
    <img src="{{ asset("../images/"item.thumbnail ) }} " alt="{{ item.alt }}" />
{% endfor %}

I have also tried

{% for item in test %}
     <img src="{{ asset("../images/"{{item.thumbnail}} ) }} " alt="{{ item.alt }}" />
{% endfor %}

but unfortunately niether of these throw any errors what so ever but they also return no content. Is this even possible and if so can anyone point me in the right direction?

p.s. I have iterated the results of item.thumbnail and also double checked that these assets have been installed into the bundle. Also the image folder is called images and not image or img.

Thank you in advance.

Edit:

For all you that wonder why i recieved no errors.... I was actually on the wrong page which is why i could see any errors. So note to all, check that your on the right page before posting that you have no errors.

Was it helpful?

Solution

Your problem is easy explained, you are not in the folder of your bundle you are in the the web folder if you use asset() in twig.

The path will be RootFolder/web if you use asset:

Store your images in the web folder e.g. in upload/images

{% for item in test %}
    <img src="{{ asset("upload/images/") ~ item.thumbnail  }} " alt="{{ item.alt }}" />
{% endfor %}

or if you do not upload new images and they are only for your template e.g. a logo,

run the command: assets:install

and change your code to:

{% for item in test %}
    <img src="{{ asset("bundles/{acme}/images/") ~ item.thumbnail  }} " alt="{{ item.alt }}" />
{% endfor %}

acme have to be the name of url bundle in lowercase and without the Bundle at the end.

OTHER TIPS

{% for item in test %}
    <img src="{{ asset("../images/" ~ item.thumbnail ) }} " alt="{{ item.alt }}" />
{% endfor %}

this should do the job. Use the ~ operator to concatenate stings.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top