phpsymfonytwigsymfony-sonatasonata-media-bundle

How to get image path in twig using SonataMediaBundle?


I'm using SonataMediaBundle to keep track of my images. I can render the image in twig using Sonata's helper:

{% media user.profilepic, 'reference' %}

This will render into an <img src="the src">

But, what I want to obtain is the naked path, so I can for instance, add a class to my img. Something like:

<img class="img-responsive" src="{{ asset(user.profilepic) }}">

Obviously, asset(user.profilepic) does not return the path, but the object, and that object doesn't seem to contain the image's path.

EDIT

Found part of my answer, as well:

{% set foo %}
     {% path image, 'small' %}
{% endset %}
<img src="{{ asset(foo) }}" alt=""/>

Apparently the output of a block can be set to a variable, then pass that to the asset function.


Solution

  • There's a documentation for helpers

    There are several options. Media helper supports specifying the class

    {% media media, 'small' with {'class': 'myclass'} %}
    

    Also you can render just a path with "path" helper:

    {% path media, 'small' %}