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

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Archive

Unsure how to display a file (image) that has been uploaded

Started by Muut Archive 10 years ago · 6 replies · 1038 views
10 years ago

I have created a member.yaml and member.html.twig file with some basic text fields like name and age which I am able to print out on the front end by looping over the following which is working fine:

---html
<h1>{{ member.name }}</h1>
<h2>{{ member.age}}</h2>

TWIG

I want to now be able to uploaded an image per member using the field type of *"file"* not the standard page media as the examples I have seen so far require the user to type the file name into the image field which isn't user friendly. When I use the field type *"file"* and upload an image I can see it being saved into the /users folder in the right place but I don't understand how to print it out.

I have tried following the same syntax as printing out normal text fields but it doesn't work e.g.

---html
<h3>{{ member.image }}</h3>

What I have noticed is the structure is different for the file upload in the group.md file in the users folder:

---html
title: Group
members:

YAML
    name: 'Bob Smith'
    age: '20'
    image:
        user/pages/01.home/_group/IMG_3850.JPG: 
            name: IMG_3850.JPG
            type: image/jpeg
            size: 1142286
            path: user/pages/01.home/_group/IMG_3850.JPG
TXT


I haven't been able to find anything in the Grav doc about how to do this but I could be misunderstanding how the file upload field is supposed to be used.
10 years ago

If you upload a file into a 'page' then it should be available as a media file, just like any other:

TWIG
{{ page.media['IMG_3850.JPG'].html }}
10 years ago

It's a modular page so I can't called the image out by its actual name, it needs to be dynamically done.

Each member has an image attached to it and an unlimited number of members could be added to the page via the admin screen.

10 years ago

How do you know which image to display then? Or do you want to display them all? If so simply loop over page.media.imagesand display them. Simply example.

10 years ago

Thanks @rhukster that example got me on the right track, each image belongs to a member, and there could be an unlimited members added via the admin page.

The solution was a loop within a loop as I needed a specific image to be linked with a specific member.

member.yaml

YAML
title: Members
'@extends':
    type: default

form:
  fields:
    tabs:
      type: tabs
      active: 1
      fields:
        members:
          type: tab
          title: Members
          fields:
            header.members:
              name: members 
              type: list
              label: Members
              fields:
                .name:
                  type: text
                  label: Name
                  help: Member's name.
                .image:
                  type: file
                  label: Image
                  multiple: false
                  destination: 'self@'
                  random_name: false
                  avoid_overwriting: false
                  limit: 10
                  filesize: 5
                  accept:
                    - image/*

member.html.twig

TWIG

<div class="members">
  <h1>{{ page.title }}</h1>  
  {# loop over each member #}
  {% for member in page.header.members %}
    <div class="member">
      <h2>{{ member.name }}</h2>
      <h3>{{ member.position }}</h3>      
      {# loop over each member specific image #}
      {% for image in member.image %}
         {{ page.media[image.name].cropZoom(600,600).quality(100).html() }}
      {% endfor %}      
    </div>
  {% endfor %}
</div>
---
9 years ago

This is pretty much what I've been looking for. I'll test it tomorrow as well :) thanks for this little loop. I assume it can be printed as a link with an image inside, no?

9 years ago

It's working for I wanted to do. Thanks a lot @steroyle for this little trick

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1297 9 years ago
Archive · by Muut Archive, 9 years ago
2 894 9 years ago
Archive · by Muut Archive, 9 years ago
2 4024 9 years ago
Archive · by Muut Archive, 9 years ago
1 2899 9 years ago
Archive · by Muut Archive, 9 years ago
3 1082 9 years ago