Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
91 lines (54 sloc) 4.52 KB


There are two twig filters to manipulate your media in templates :

###The vlabs_media() filter

{{ <media_object>|vlabs_media(<template>, <options_array> }}
  • media_objet : for exemple, entity.image
  • template : Here you can either use the identifier of the template defined in the configuration, either the full path to a template (if you do not want to set it in the configuration).

So if your configuration looks like :

        form_doc: MyFooBundle:Templates:form_doc.html.twig
        image: MyFooBundle:Templates:image.html.twig

Those 3 calls will works :

{{ entity.image|vlabs_media('image') }}
{{ entity.doc|vlabs_media('MyFooBundle:Templates:form_doc.html.twig') }}
{{ entity.resume|vlabs_media('MyFooBundle:Templates:pdf.html.twig') }}

Note that the third file must exists.

Using media properties and templates options

The second parameter is an array. You can pass all the data that you want, they will be accesible in the template. You also have access to all the media properties.

{{ entity.image|vlabs_media('image', { name : 'John Doe' } ) }}

will give you access in the MyFooBundle:Templates:image.html.twig template to :

<img src="{{ media.path }}" alt="{{ }}" />

If your media is extending from Vlabs\MediaBundle\Entity\BaseFile, you will also have access to all this properties.

There are six templates provided by default with the bundle, you can (must?) replace. Just change the path of template in the configuration.

default: VlabsMediaBundle:Templates:default.html.twig  # return the media path
image: VlabsMediaBundle:Templates:image.html.twig  # return a `img` tag with path as src
form_doc: VlabsMediaBundle:Form:form_doc.html.twig # return a `a` tag with path as href and 'View' as label 
form_image: VlabsMediaBundle:Form:form_image.html.twig # return a `img` tag with path as src
vlabs_file: VlabsMediaBundle:Form:vlabs_file.html.twig
vlabs_del: VlabsMediaBundle:Form:vlabs_del_file.html.twig

The last four lines are template used in forms.

###The vlabs_filter() filter

For all your images, you can use the vlabs_filter() filter to trigger resizing & caching for your media :

{{ entity.image|vlabs_filter(<filter_name>, <options_array> ) }}

The bundle provides two buit-in filters :

  • resize wich handle 4 parameters : width (numeric) / height (numeric) / upscale (boolean) / keepRatio (boolean)
  • crop wich handle 2 mandatory parameters : width (numeric) / heigth (numeric)

Note that you can combine filter & media, every filter deals with a BaseFileInterface :

{{ entity.image|vlabs_filter('resize', { width : 300, height : 300, keepRatio : true } )|vlabs_media('image') }}

###Using form templates & twig form helper

To manage your media in forms, two templates are available:

Both templates are tagged as form.type and can be used in form process.

The vlabs_file template is using a helper called getBaseFile to retrieve the BaseFileInterface object before calling the twig filters.