Sliq
Sliq

Reputation: 16494

How to show a dynamic image URL with Twig?

I'm kindly asking for help with displaying a dynamic image URL within a Twig template like this:

// non-working pseudocode, myImageFilename contains image filename
{% image '@AppBundle/Resources/public/images/' + myImageFilename %}
    <img src="{{ asset_url }}" />
{% endimage %}

but Twig only allows static filename, like this (taken from documentation). I've tried the concatinator ~ like

{% image '@AppBundle/Resources/public/images/' ~ myImageFilename %}

but it also doesn't work,

{% image '@AppBundle/Resources/public/images/example.jpg' %}
    <img src="{{ asset_url }}" alt="Example"/>
{% endimage %}

Is there a possiblity to use dynamic image URLs with this Twig image logic ?

Upvotes: 4

Views: 5571

Answers (2)

shivanshu patel
shivanshu patel

Reputation: 778

For now, I don't think it is possible do it. The reason behind this is that Assetic is run upfront to dump the asset.

It is possible through this way :

<img src="{{ asset('bundles/appbundle/Resources/public/images/'~myImageFilename }}" alt="Example"/>

Upvotes: 5

mduvernon
mduvernon

Reputation: 498

you cannot acceed bundle directory from browser client (.htaccess deny from all), you have to load your image from ./web/ directory instead

Upvotes: 1

Related Questions