Getting page content element from page collection


I’m very new to Grav and looking for a way to extract page child element from page.collection array, alike getting images using a loop:

{% for p in page.collection %}
    <li><a href={{ p.url }}>{{ p.title }}</a></li>
    {% for image in %}
      <li><img src={{ image.url }} /></li>
{% endfor %}

but instead something alike this:

{% for p in page.collection %}
   {% for child in p.content %}
      <li>{{ child }}</li>
    {% endfor %}
{% endfor %}

while expecting to get a list of content child elems like <p>..</p> and such.

Looking for Twig ways of doing this i found this that mentions Twig tweak module - would it be an option within Grav and if, how would one add a Twig module to Grav?

Any other solution would be just as welcome too :wink:


@binary.koala, Do you want to loop through all pages in page.collection and list all children of the current page in the loop?

If so, you could try:

{% for p in page.collection %}
   {% set pChildren = p.evaluate(['@self.children']) %}
   {% for child in pChildren %}
      <li>{{ child.title }}</li>
    {% endfor %}
{% endfor %}

If not, please explain what you are trying to achieve.

Hi, thanks for the response.

What I would like to achieve is to be able to extract partial content of child, say first <p></p>.

{{ child.summary }} gets me pretty close but i would still like to be able to pick-out only the first <p></p> HTML element from that output. Should I be using a custom filter?


Should I be using a custom filter?

There is a Twig function regex_match you could try.

According the docs:

A helpful wrapper for the PHP preg_match() method, you can perform complex regular expression match on text via this filter:

regex_match('', '@^(?:http://)?([^/]+)@i')

    [0] =>
    [1] =>

Why you don’t use the basic summary delimeter? Pages | Grav Documentation