contao icon indicating copy to clipboard operation
contao copied to clipboard

Twig file filter

Open marcuslelle opened this issue 1 year ago • 5 comments

Description

It would be nice to have a filter in Twig templates that allows you to retrieve e. g. the path of a file from its uuid.

There is a solution in the 0.1 Theme https://github.com/contao-themes-net/zero-one-theme-bundle/blob/master/src/Twig/FileExtension.php

marcuslelle avatar Oct 24 '24 11:10 marcuslelle

Could also be a function instead of a filter I think.

ausi avatar Oct 24 '24 11:10 ausi

What's your use case? Sounds like the endpoint maybe should use the VFS and then support Uuid|string as an input?

m-vo avatar Oct 24 '24 13:10 m-vo

My use case would be one of my own templates, e. g. in the RSCE extenstion. Right now I would like to load several videos as background. But I could think of PDF files to show up in an own template.

marcuslelle avatar Oct 24 '24 13:10 marcuslelle

Just to give you some background why I'm hesitating to just expose getting a UUID without a context: The question is what 'kind of path' you would assume to get back.

For instance, if you directly asked the mount manager to resolve <foo-uuid> you would get a path files/images/foo.jpg (effectively a root-storage-relative path). If you'd instead used $filesStorage->get(<foo-uuid>)->getPath(), you would get images/foo.jpg - the files-storage-relative path. And if you asked the backup storage, you'd get no path at all (wrong scope).

m-vo avatar Oct 24 '24 13:10 m-vo

As briefly discussed on slack: We could create readonly view of the files storage and expose that one via the global contao object.

Usage would then be like so:

{{ contao.filesStorage.generatePublicUri(<uuid>) }}
{{ contao.filesStorage.resolveUuid(<uuid>) }}

{# You could also do other things #}
{% set file_item = contao.filesStorage.get('…') %}
{% if file_item.isImage %}
    {% with {figure: figure(file_item, [200, 200])} %}
      {{ block('figure_component') }}
    {% endwith %}
{% endif %}

Things to consider:

  • How to deal with exceptions? One solution could be a VFS wrapper, that catches exceptions (or the UnableToResolveUuidException) and maybe additionally limits the amount of exposed functions.
  • We are probably going to need a |string_to_uuid filter as well, so that people could use their string/binary string uuids as an input.

m-vo avatar Oct 24 '24 13:10 m-vo

How to deal with exceptions? One solution could be a VFS wrapper, that catches exceptions … and maybe additionally limits the amount of exposed functions.

Yes that makes most sense I think :+1:

ausi avatar Nov 14 '24 15:11 ausi