0

Here is my current code:

{% for module in page.collection() %}
    {% set index = loop.index %}
    {{ module.content|raw }}
{% endfor %}

I'd like to access index inside the module.html.twig, or even better, the entire loop variable.
How do I do that?

Norman
  • 785
  • 7
  • 27
  • What is `module.html.twig`? – DarkBee Dec 17 '18 at 12:51
  • @DarkBee It's the module which is being rendered at each iteration of the for-loop. More: https://learn.getgrav.org/content/modular - It's basically a regular Twig file – Norman Dec 17 '18 at 13:16
  • So you want to use the `loop`-variable inside `module.content`? – DarkBee Dec 17 '18 at 13:19
  • @DarkBee Yes, exactly! – Norman Dec 17 '18 at 13:24
  • 1
    I don't use `grav` myself but, from what I can see it's worth a try to do the following: `{{ module.content({ 'loop': loop, }) }}` to pass the loop to your modular page – DarkBee Dec 17 '18 at 13:32
  • @DarkBee I found a solution by myself, but out of curiosity I also gave your solution a shot. Unfortunately, it didn't work. Thanks for the help, though! – Norman Dec 17 '18 at 13:43

1 Answers1

2

I found it myself:

{% for module in page.collection() if not module.header.visible is same as(false) %}
    {% include module.template ~ '.html.twig' with {'page':module, 'loop':loop} %}
{% endfor %}

This loop willautomatically grab the template which is linked to the modular page and pass the required variables down. Also, the loop will only include modular subpages which are not hidden. Great, isn't it?

Norman
  • 785
  • 7
  • 27