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

Twig syntax to display uploaded custom files

Started by Muut Archive 10 years ago · 2 replies · 459 views
10 years ago

Grav newbie and running into an issue where I can't figure out how to grab and display a custom file that I uploaded earlier through the admin CMS.

YAML
header.review_basics_masthead:
type: file
label: Masthead
destination: 'uploads/reviews'
blueprint: 'themes.mytheme'
accept:
- image/jpeg

It's essentially this code, but modified slightly as I don't want to showcase the site name just yet (it's working and the CMS is keeping the record of the file upon reload, so that's not the issue).

I turned on debug and I see the array object if I:

TWIG
{{ dump(page.header.review_basics_masthead) }}
JS
array:1 [
  "uploads/reviews/review.jpg" => array:6 [
    "name" => "review.jpg"
    "path" => "uploads/reviews/review.jpg"
    "type" => "image/jpeg" 
    "size" => 65756
    "file" => "uploads/reviews/review.jpg"
    "route" => null
  ]
]

So I know it's working, but I can't figure out the proper syntax to actually get the image from this. I thought it might have been adding .path, .file, etc., but those all were NULL, so that isn't it. Couldn't find anything in the documentation either.

10 years ago

Not sure how I can close out topics, I ended up using the pagemediaselect functionality here instead.

9 years ago

Hey! For what is worth... I found a workaround:

I also tried {{ page.header.thumbnail[0] }} and similar, but no dice.

This is how I did it. Just traverse the uploaded files array:

TWIG

{% set thumbnail = "" %}

{% for key, item in page.header.thumbnail %}
  {% set thumbnail = item.path %}
{% endfor %}
---

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1344 9 years ago
Archive · by Muut Archive, 9 years ago
2 930 9 years ago
Archive · by Muut Archive, 9 years ago
2 4058 9 years ago
Archive · by Muut Archive, 9 years ago
1 2943 9 years ago
Archive · by Muut Archive, 9 years ago
3 1116 9 years ago