Skip to content
onadrog edited this page Sep 14, 2021 · 2 revisions

Twig extension:

Usage:

{% extends 'base.html.twig' %}

{% block body %}
    {{ image_converter_img(foo) }}
{% endblock %}

Will return an <img> tag or a <picure> tag as follow:

<!-- Example without the original file ('keep_original' config vars set to false) -->
<body>
  <img
    class="image_converter_img"
    alt="the alt name provided"
    src="/uploads/media/foo.webp"
    width="150"
    height="150"
    loading="lazy"
  />
</body>

<!-- Example with the original file ('keep_original' config vars set to true) -->
<body>
  <picture>
    <source srcset="/uploads/media/foo.webp" type="image/webp">
  <img
    class="image_converter_img"
    alt="the alt name provided"
    src="/uploads/media/foo.png"
    width="150"
    height="150"
    loading="lazy"
  />
  <picture>
</body>

Passing parameters:

{% extends 'base.html.twig' %}

{% block body %}
    {{ image_converter_img(foo, false, 'my_class_name', 'file') }}
{% endblock %}

Will return an image tag as follow:

<body>
  <img
    class="my_class_name"
    alt="the alt name provided"
    src="/uploads/media/foo.webp"
    width="150"
    height="150"
  />
</body>

Parameters:

Parameter Type Required Default
$val object true null
$lazyload bool false true
$className string false image_converter_img
$property string false file

function signature:

image_converter_img(object $value,bool $lazyLoad = true,string $classname = 'image_converter_img', string $property='file'): string

Note:

If you have changed the default entity property $file where the ImageUploadProperties is mapped on you MUST pass this new property new to the extension.

Clone this wiki locally