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

Reference current page folder in theme twig

Started by Muut Archive 9 years ago · 2 replies · 716 views
9 years ago

I'm putting together my first Grav site and rather liking it but I've hit a problem I just can't seem to figure out. I do have a working solution but it requires a hard typed path that would break if I ever moved the page folder to a different location. That is obviously not the best of solutions.

I have the following set up

user/pages folder

YAML
pages
  - 01.bobs-burgers
    - default.en.md
    - burger-header.jpg

inside default.en.md

YAML
title: Kitchens
banner: 
  enabled: true
  url: 'burger-header.jpg'

inside default twig

TWIG

    {% if header.banner.enabled %}
        <div class="banner"{% if header.banner.url is not empty %} style="background-image: url('{{ header.banner.url }}')"{% endif %}></div>
    {% endif %}
--- 

What I am trying to do is have an image defined within the default.en.md file be placed within the theme twig. The problem is the image is in the pages folder and I can not get the correct path to it. I can hard code it in the header to be url: '/user/pages/01.bobs-burgers/burger-header.jpg' but then if I ever move the folder the path breaks.

I'm looking for either something that can go in the .md header like url: $pageFolderPath.'/burger-header.jpg' or something that can go in the twig file like url="{{ pageFolderPath }},{{ header.banner.url }}" where pageFolderPath is resolved to "/user/pages/01.bobs-burgers/"

or maybe I'm doing this in a bone headed way and I need some straightening out XD
9 years ago

this is not reliably working e.g. when using SVG inside an object - try {{ page.relativePagePath }}

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