Skip to content
Grav 2.0 is officially stable. Read the announcement →
Archive

How to display an image that is upload via file field of a custom page form

Started by Muut Archive 10 years ago · 3 replies · 2542 views
10 years ago

So I've been working with grav cms lately. Unfortunatly I can't find any answers to some problems I have.

I'm trying to upload a image with a custom blueprint form, using the file field.

this is my form soure code

YAML
title: homeref
'@extends': default

form:
    fields:
        tabs:
            fields:
                content:
                    type: ignore
                options:
                    type: ignore
                advanced:
                    type: ignore
                referenties:
                    type: tab 
                    title: Referenties
                    fields:
                        header.referenties:
                            type: list
                            label: Referenties
                            fields:
                                .image:
                                    type: file
                                    label: referentie IMG
                                    multiple: false
                                    destination: 'theme@:/images'
                                .text:
                                    type: text
                                    label: Referentie beschrijving

How can I display this image with twig?

10 years ago

Ahh, sorry. Different destination you save the file to.
See here: https://learn.getgrav.org/themes/theme-vars#theme_url-variable
And here: https://learn.getgrav.org/themes/twig-filters-functions#url

Mhm, I don't know exactly if this will work, but should be something like this for your case:

TWIG
{% for img in header.referenties %}
   <img src="{{ url('theme://images/) }},{{ img.image }}" alt="{{ img.text }}" />
{% endfor %}

Could also be img.image.name

Very helpful in general is installing the developer tools and tracing out vars to find out what it is or if that array stores relevant information

TWIG
{% for img in header.referenties %}
   {{ dump( img) }}   <!-- logs maybe an array? -->
   {{ dump( img.name) }}   <!-- logs maybe the name ? -->
   {{ dump( img.url) }}   <!--  logs maybe the url -->
   {{ dump( img.text) }}   <!-- should log your 'Referentie beschrijving' ? -->
{% endfor %}

The documentation is pretty well. Although sometimes I miss in the blueprints form examples their relevant links to "how to" examples to get that data working within twig.

10 years ago

Thanks for replying!

I am kind of new to grav and I didn't know about the debug functionality. Thanks for that!

I finally found a solution thanks to debugging:

In my blueprint I changed the destination to:

TXT
destination: 'self@'

and this is the code in my template file:

TWIG
{% for data in header.referenties %} {# Loops over the values of referenties -> referenties is an array #}
    {% for img in data.image %} {# Loops over the values of image -> image is also an array #}
        {{ page.media[img.name].html }} {# Displays the image #}
    {% endfor %}
{% endfor %}

Btw I totally agree with you about the documentation. Let's hope it gets better in the future.

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1354 9 years ago
Archive · by Muut Archive, 9 years ago
2 935 9 years ago
Archive · by Muut Archive, 9 years ago
2 4064 9 years ago
Archive · by Muut Archive, 9 years ago
1 2951 9 years ago
Archive · by Muut Archive, 9 years ago
3 1119 9 years ago