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

Pages object from modular template?

Started by Muut Archive 10 years ago · 8 replies · 548 views
10 years ago

I can't figure out how to access the pages object from a module template as such:

TWIG
{% for page in pages.children %}
  {{ page.title }}
{% endfor %}

By modular template I mean:
/user/themes/theme-name/templates/modular/module-template.html.twig

10 years ago

pages.children accepts a path, the page you route you want to inspect. see API

page.children on the other hand returns the children of the current page. API

On a modular child page, page.children usually is not set, do you want the parent page children? In this case you can get page.parent.children

10 years ago

Ok I think we're on the right track. Actually I want the root for all the pages /users/pages/ but `pages.find('/').children' doesn't return anything.

10 years ago

pages.find('/') returns the home page. You want pages.all

10 years ago

I guess I'd need a code example to make pages.all work, cause it's not.

10 years ago

My mistake. pages is a link to the root page (/users/pages/) so you can do

TWIG
{% for page in pages.children %}
  {{ page.title }}
{% endfor %}
10 years ago

lol, we've come full circle.. if you see the top of my post, I did exactly that. The problem is that it doesnt work from a template in templates/modular/template.html.twig

10 years ago

Ouch!
You're right it seems the pages object is not directly accessible in modular childs. Use

TWIG
{% for page in grav.pages.root.children %}
  {{ page.title }}
{% endfor %}
10 years ago

THANK YOU! Jeez that was messing with my head. You're a god among insects! :)

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1359 9 years ago
Archive · by Muut Archive, 9 years ago
2 936 9 years ago
Archive · by Muut Archive, 9 years ago
2 4066 9 years ago
Archive · by Muut Archive, 9 years ago
1 2955 9 years ago
Archive · by Muut Archive, 9 years ago
3 1121 9 years ago