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

Use page resources within inline html on a page

Started by rappluk 10 months ago · 10 replies · 376 views
10 months ago

So, I’ve got a need for a specific object, that is represented in markdown as html like so

MARKDOWN
<object width="400" height="500" type="application/pdf" data="page://manual.pdf">

    <p>Ups, das PDF der Anleitung kann nicht angezeigt werden...</p>

<a href="page://manual.pdf">Hier herunterladen</a>

</object>

However, quite obviously the href and data attribute doesn’t resolve the page://manual.pdf link.

What is my best course of action here? Switch to modular and just use a “object module” here? Any other way to link to / display my pdf properly within that page?

Thanks a lot 🙂

10 months ago

IIRC, you can access any uploaded file via page.media 🤔

10 months ago

But isn’t that in twig then? How would I do the same in markdown?

10 months ago

Sorry, misunderstood the question

So to render just a link, it's quite simple:

MD
[Link text](file_on_page_pdf)

But if you want to render an actual preview of the PDF, I assume you will need some plugin. Don't know if any exist for this kind of output

10 months ago

I know how the markdown link is supposed to work.
I guess maybe what confuses us is the following: Can twig variables be used in the html-within-markdown snippet above?
so in essence, could I have data="page.media.whatever(‘manual.pdf’)"> in that html snippet?

10 months ago

In the page frontmatter add:

YAML
---
process:
    twig: true
---

In admin it's on the Advanced tab

I have used Twig in MD like this and tested just now with {{ page.title }} and it works. I don't remember the structure of page.media, so I didn't test that

10 months ago

@rappluk:
<object width="400" height="500" type="application/pdf" data="page://manual.pdf">

isnt this just html? i dont see where it triggers to render something as twig.

i would use something like {{ }} when i want to use twig, and as Karmalakas said i would active twig for that page.

another way

why would i use twig for that? the url of that file would be something like ((url of that page) + (file name)) ex: mypage.com/apage/manual.pdf so i would just put this url there.

10 months ago

Well I want to avoid hardcoding that URL, since the page name, slug, you name it could change later, or between localisations.

Whatever that is called, twig, markdown link, I want whatever page://manual.pdf represents to be rendered by grav, so even if anything changes about my structure, it will still return the location of the pdf in the current page.

10 months ago

So did you try with twig processing enabled?

10 months ago

@rappluk, First of all, to me, your requirements could be stated a tad more specific. Currently, a lot of questions arise...

Anyway...

Have you had a chance to read the docs on Media?

It provides several examples showing how to get the url of a media file inside the folder of a page, or anywhere else, using Twig (which can be used inside Markdown).

You also might have a look at Grav's API which shows the interfaces the Media object implements.

Some examples to get a media file from within the current page's folder:

TWIG
# Get any media file
<a href="{{ page.media['manual.pdf'].url | e }}">Named media file</a>
<a href="{{ (page.media|first).url | e }}">First media file</a>

# get image files
<a href="{{ page.media.images['myimage.png'].url | e }}">Named image file</a>
<a href="{{ (page.media.images|first).url | e }}">First image file</a>

# Get data/document files like pdf
<a href="{{ page.media.files['manual.pdf'].url | e }}">Named file</a>
<a href="{{ (page.media.files|first).url | e }}">First file</a>
👍 1
last edited 10/24/25 by pamtbaau
10 months ago

@rappluk, Any progress you would like to share? It would benefit the community.

Suggested topics

Topic Participants Replies Views Activity
Support · by Paul Hodges, 4 weeks ago
19 483 5 days ago
Support · by Lauw, 7 days ago
2 99 6 days ago
Support · by Anna, 2 weeks ago
4 426 1 week ago
Support · by gtx, 4 weeks ago
4 377 3 weeks ago
Support · by Anna, 1 month ago
9 484 3 weeks ago