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

Cannot link media using absolute or relative URLs

Started by Muut Archive 11 years ago · 7 replies · 1165 views
11 years ago

Hello. From Grav Documentation - Image Linking I read that it should be possible to traverse up from page folder into another folder either using slugs or folder names to reference media files. Another option is to use absolute URL starting from pages folders. I am trying to do that in a twig template, however neither method works and I am only able to reference images from the page folder.

When seeking solution, I found some discussion on Media Site on Grav, where there is a suggestion to use page.find("whetever/url"). Is this the way to go? Why I am unable to do it like the Doc says? Or is it supposed to work not in a twig template, but only when referencing from a markdown file?

Thanks for support :)

11 years ago

If you can zip up your entire site (put it on dropbox/googledrive/etc) and point me to the page where your having the trouble I can have a better look. I really need to see your site structure to see why your having trouble with the linking.

11 years ago

Ok, sorted it for you:

1) Your images folder was not in pages structure so i moved it into user/pages/
2) Your imagelin: header variable had the page path and the media file, i changed this to just the media file:

TXT
imagelink: "sobboriko-zhizelle.jpeg"

3) You were tyring to find a page called images/sobboriko-zhizelle.jpeg which of course, did not exist. I changed this to:

TWIG
{{ page.find('/images').media[page.header.dog.imagelink].html() }}

So it finds the images page (now successfully), then looks in the media of that page and finds the image file (now just sobboriko-zhizelle.jpeg) then outputs the html.

Now it's working fine :)

I Zipped up my local copy too incase you want to use this:

https://dl.dropboxusercontent.com/u/73693/cannot-link-media-using-abs.zip

11 years ago

Wow, what a qucik response - thanks.

I get the logic, thanks. However I have an issue here - what if I want to have subfolders underneath images, because loads of images should be stored and I want to group them so that it would be more easily to manage. Because it is a subfolder, it is a different page and page.find will return null.

Is there a way to provide imagelink: "/images/folderA/imagea.jpeg" for one dog page and imagelink: "/images/folderB/imageb.jpeg" for the other and let the twig template lookup the image based on the path?

11 years ago

Well you have lots of options really:

1) add another header variable for imagepage like:

YAML
imagepage: "/images/folderA"
imagelink: "imagea.jpeg"

Then combine those two in the template:

TWIG
{{ page.find('page.header.dog.imagepage).media[page.header.dog.imagelink].html() }}

2) You can simply use the markdown for the image plus text in the body of the .md file:

TXT
![](/images/folderA/imagea.jpeg)

The support for page and link URLs is actually contained within the markdown link trait, so to use that built-in functionality where it finds the page and image automatically, you need to do it from markdown. The way you had it and the way I fixed it, was in Twig, and that can be done, but its not automatic

11 years ago

Okay, I understand. Thanks for your time.

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1327 9 years ago
Archive · by Muut Archive, 9 years ago
2 921 9 years ago
Archive · by Muut Archive, 9 years ago
2 4050 9 years ago
Archive · by Muut Archive, 9 years ago
1 2928 9 years ago
Archive · by Muut Archive, 9 years ago
3 1107 9 years ago