6

I want to store full path of image (sonata media bundle) into variable in twig. Is that possible?

If I write:

{% set pic = path item.image, 'big' %}

it throws me an error: Unexpected token "name" of value "item" ("end of statement block" expected) ...

If I write:

{% set pic = item.image %}

then it works, but it stores only name of the file, not full path.

repincln
  • 2,029
  • 5
  • 24
  • 34
  • Check this question, there are two extensions doing the job: http://stackoverflow.com/questions/16637616/sonata-media-bundle-acces-media-url/28380265 – Thomas Kekeisen Feb 07 '15 at 08:56

3 Answers3

6

Why don't you do like this ?

{% set rendered %}{% path item.image, 'big' %}{% endset %}
....
Here is my path {{ rendered }}
Azam Alvi
  • 6,918
  • 8
  • 62
  • 89
lefakir
  • 3,983
  • 4
  • 27
  • 37
1

There is not such a function available (there is a path() function to generate routes). You have to create your own twig extension with this custom function. Read all about that in the documentation.

Wouter J
  • 41,455
  • 15
  • 107
  • 112
0

Perhaps you can solve with:

<img src="{% path media, 'small' %}" data-href="{% path media, 'big' %}">

user511564
  • 366
  • 4
  • 5