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

Theme twig code to include page from the /pages area?

Started by Muut Archive 11 years ago · 11 replies · 1074 views
11 years ago

I don't know why I can't get my head around how to do this, it seems like it would be simple enough. In a theme template I'm writing, I want to {% include %} a file not from the theme area but from the /pages area. Might someone help me out?

11 years ago

Damn I should have put this in the theme section, sorry. If it can be moved, please do.

11 years ago

What are you trying to do, exactly? Including a .md file?

11 years ago

In my theme, I need to pull in a specific sub page to the current page if it exists. It didn't occur to me to use a for loop because the sub page was always going to have the same name and I was looking for only one specific page, so I was getting way hung up on the pathing using %include% in the template.

Basically it's going to be a file that will serve as a sub menu relevant only to the page you're looking at. I wanted to do this from the template, not from the current page's .md file.

/pages
....01.page1/
....02.page2/
.... ....menu/
.... ....other-sub page1/
.... ....other-sub page2/
....03.page3

When the template for 02.page2 loads the content, it would see the menu/ sub page and load the .md inside it.

I think the for loop would work great, just check the value of p for "menu" and then include it if it matches?

11 years ago

I would create a twig file in my theme that basically found the pages in question and looped over them to create the sub menu.

Then i would include this sub menu twig template file from the template file that 'only' my pages that should have the sub menu used.

11 years ago

This is really a common sort of thing. There is a similar approach in the blog skeleton with anitmatter that only loads a sidebar for pages in the blog, so that's the blog and item templates.

11 years ago

Yeah I was looking for a code example in exactly that place, I think my mind was approaching it differently and I wasn't looking for the method you describe, thank you, big help!

11 years ago

@rhukster, I've used that trick for my website. When I looked at the code I noticed the find method is deprecated: is there a mistake or I should look to another method?

11 years ago

Page.find() is not deprecated. Where are you seeing that?

11 years ago

That's most probably a forgotten line. Feel free to use that, I'll remove @deprecated from the method signature.

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1334 9 years ago
Archive · by Muut Archive, 9 years ago
2 924 9 years ago
Archive · by Muut Archive, 9 years ago
2 4055 9 years ago
Archive · by Muut Archive, 9 years ago
1 2938 9 years ago
Archive · by Muut Archive, 9 years ago
3 1111 9 years ago