Shortcodes refer to wrong subpage when used in loop.
I am looping over a page's subpages using {% for p of page.collection %} and then dump the shortcodes using
{{ dump(p.contentMeta.shortcodeMeta.shortcode) }}.
The dump of the shortcode object always contains the values of the very first subpage (while p.raw_content is correct). I think that's ... wrong. Or am I doing it wrong? I would assume, that p.contentMeta.shortcodeMeta should always be the shortcodes of the page that p refers to.
TLDR:
- Empty cache, browse to parent ->both children in collection show section of child 1
- When child page is updated -> its section is refreshed
- Empty cache, browse to parent ->both children in collection show section of child 1 again
- Empty cache, then browsing to either child 1 or 2, and then to parent -> both sections are shown
Steps to reproduce:
- Fresh Grav 1.6.26 installation
- Install plugin shorcode-core
- Define collection with 2 child pages
- Content child 1:
[section name="text"]Section from child 1[/section] - Content child 2:
[section name="text"]Section from child 2[/section]
- Content child 1:
- Twig contains:
{% for child in page.collection %} {{ child.contentMeta.shortcodeMeta.shortcode.section.text }}<br> {% endfor %} - Clear cache ->
Section from child 1 Section from child 1
- Changed child 1 ->
Section from child 11 Section from child 1
- Changed child 2 ->
Section from child 11 Section from child 22
- Clear cache ->
Section from child 11 Section from child 11
- Clear cache, browse to child 1 or 2 first, then to parent ->
Section from child 11 Section from child 22
So you'll have to evaluate the Page by forcing the caching by navigating there? That seems like a very painstaking process. I suggested on Discord that he use FrontMatter instead as that's more readily available and easier to maintain validation for in editing from Admin.
I have the same issue with a template which i wrote in 2018. It was working, when it was delivered. So this might help. Sorry but i cannot say when exactly the code didnt work anymore. I've got the call from my client 2 weeks ago.
This issue wasnt fixed for month now, so i guess it will not fixed soon. I hope this can be helpful.
{% for _p in page.collection %}
{% set _s = _p.contentMeta.shortcodeMeta.shortcode.section %}
<h2>{{_s.headline}}</h2>
.
..
...
<p>{{_s.description}}</p>
{% endfor %}
Page structure
01.home
_first
default.md
_spotlight
_spotlight_item_1
spi-modular.md
_spotlight_item_2
spi-modular.md
_spotlight_item_3
spi-modular.md
sp-modular.md
_last
The shortcodeobject got the sections of the sp-modular.md and of the first spi-modular.md for every loop. As is said, the code was working in 2018, when it was delivered to the client.
I like the concept of sections. It's easy to read in the backend (especially when it comes to long text) and it's the common way to put content into grav. Sure u can use the Frontmatter instead, but imo the frontmatter should be used for settings not for content.
Best regards.