简体   繁体   中英

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.

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' %}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM