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.

Support

Redirect page title to file

Started by ivankaya 2 years ago · 4 replies · 267 views
2 years ago

Hi, I have been struggling to find a way to have my page title redirect to open a PDF file in the browser.

What I have (obviously) been able to do is insert [file](file.pdf) into a default Grav themed page, and clicking on the link in the page, the browser opens the PDF.

Is there a way to do the same immediately from the page title? So that clicking on the page title doesn't open the page, but directly the pdf?

I could use the redirect frontmatter, but then I can't seem to reference a local file, but would have to upload the PDF to some other cloud provider and create a share link.

Thanks!

2 years ago

Which theme are you using and could you please indicate which part of your theme page contains what you refer to as "page title"? That term is open to different interpretations.

2 years ago

Thanks! I'm using the default Grav theme, Quark, without any modifications. The page title is the text that I put in the --- title: <page title> ---- frontmatter.

Quick update: I have been able to achieve what I want with having the following frontmatter:

YAML
---
title: <page title>
redirect: 'http://localhost:8080/<path_to_pdf.pdf>'
---

(page still in development, so currently runs in localhost. I guess I'll have to change it to the deployed url...)

I'm wondering whether there is a method though that is more elegant than hardcoding the URL into the page frontmatter.

Thanks!

2 years ago

Probably you will need make some changes in your twig template, for example, downloads.html.twig in this way:

TWIG
<a href="{{ file.url }}" download>{{ page.title }}</a>

Your file.url may be your first file of page.media {{page.media.all|first}} if you have only one pdf per page. Otherwise you have to define this file in the blueprint of downloads page.

This is indicative. It might be interesting for you to take a look at Grav's documentation on blueprints and media.

Surely someone on this forum will have a more specific idea and can help you much better.

Suggested topics

Topic Participants Replies Views Activity
Support · by Thomas, 1 week ago
2 57 15 hours ago
Support · by Anna, 3 days ago
2 65 18 hours ago
Support · by Justin Young, 19 hours ago
1 33 18 hours ago
Support · by Duc , 1 week ago
2 68 5 days ago
Support · by Colin Hume, 1 week ago
2 60 6 days ago