Reuse module in other pages, related to complex collections

Hello Grav users,

I read a topic about reusing modules in another modular pages and read about Complex Collections and I can’t make it work on a site I’m working on.

Here’s the case: I have a modular site with a form I’d like to include in most (not all) the pages of the site. The form is in the home page as a module like:

  • home
    – _section1
    – _section2
    – _section3
    – _form (the one I want).

On another page I’d like the home page’s form sometimes at the bottom but not always.

I tried a complex collection on the another page, let’s call it about-us and this would be it’s structure:

  • about us
    – _section1
    – _section2
    – _form (from home page)
    – _section3

On the default.md file of about us I can put the modular content like this

--
content:
     items:
          - '@self.modules' (was self.modular on grav 1.6)
          - '@page.page': '/home/_form-1'
     order:
          by: default
          custom:
               - _section1
               - _section2
               - _form-1 (or how do I call this???)
               - _section3
--

This approach doesn’t work. This doesn’t display on the site and when I edit the page on the admin, I get a server error. Even not using the custom order, just by adding the @page in the collections make the error appear. I added the slug the admin gave me, but I don’t know what else can be happening.

What can I do? I even tried creating a dummy modular _form-1 and on its .md file use the page collection but that didn’t work either.

Thanks in advance.

You might want to take a step back and use the form and collection(s) in normal Grav pages instead of modular pages. Once that is all working you can change to using modular pages.

It seems to me that using collections in the page frontmatter is just too complicated and might not be possible at all. I suggest you make the switch to using or creating your collections in Twig. In a Twig template you can use and process collections as you need.

A very helpful - I think - post about using a form in Twig is Example for Twig Form

Finally once you get to understand Twig templating in Grav you’'ll probably won’t need to use modular pages at all since what makes a modular page a modular page is a Twig template named, you guessed it, modular.

I don’t think that would work either. The twig templates are just that, templates, they just print what’s on the MD files and it’s a generic twig template for the site and the difference in content is dictated by the markdown files. I couldn’t make a twig for every use case because the page I want to embed, not always is at the end of the page above the footer, but sometimes in the middle and sometimes not there at all.

That’s why I thought on using a complex collection, but apparently it doesn’t work.

Also, I’m taking over a site made by someone else. I’d have to rebuild the whole site (more than 50 internal pages (not to mention the modular pages that made up every one of that 50+). That’s a huge endeavor that I’m too new to take.

Thanks for your help.

I’m not sure what you mean by “The twig templates are just that, templates, they just print what’s on the MD files and it’s a generic twig template for the site”.

Is it your opinion or knowledge about Twig in general or are you referring to the existing Twig templates which already exist in the site you are trying to rebuild?

What I mean is that in the theme the site I’m working on, the templates just print what the MD file tells it to print.

I don’t have twig templates for specific use cases. Just twig files for modular pages and I wouldn’t know how to use a specific twig template to display a different collection.

I do not intend to modify the workings of the whole site to achieve what I want: to reuse a .MD file in another pages of the site instead of copying it many times as needed. The MD file in question is a contact form that should appear on many internal pages of the site.

Cheers!

@el_charlie,

On the default.md file of about us I can put the modular content like this
[…]
This approach doesn’t work.

For a modular page, the top level file should be named modular.md, not default.md.

In your case, the default template will be used instead of the modular template.

Doesn’t make a difference in my case.

The theme of the site doesn’t have a template called modular. It has the default but looking at the code, it’s similar as the base theme’s modular template.

The problem I’m facing is that I can’t make a complex collection with the modular items AND another page at the same time. Also, if I could, I wish to set a custom order putting the extra page between the items of the modular collection.

Thanks anyway.

@el_charlie, You haven’t shared anything about your environment, so I cannot comment on that.

Here’s is working example using skeleton On-Page Site:

  • Create fresh install of skeleton
  • Copy folder /user/pages/01.home as /user/pages/02.other
  • Remove file /user/pages/02.other/02._highlights
  • This should now be the /pages folder tree
    user/pages
    ├── 01.home
    │   ├── 01._hero
    │   │   ├── header.jpg
    │   │   └── hero.md
    │   ├── 02._highlights
    │   │   └── features.md
    │   ├── 03._callout
    │   │   ├── jeep.jpg
    │   │   └── text.md
    │   ├── 04._features
    │   │   └── features.md
    │   └── modular.md
    └── 02.other
        ├── 01._hero
        │   ├── header.jpg
        │   └── hero.md
        ├── 03._callout
        │   ├── jeep.jpg
        │   └── text.md
        ├── 04._features
        │   └── features.md
        └── modular.md
    
  • Modify the collection in /user/pages/02.other/modular.md as follows:
    content:
      items: 
        - @self.modular
        - @page.page: /home/_highlights
      order:
        custom:
          - _hero
          - _callout
          - _features
          - _highlights
    
  • Browse to https://yourdomain/ and see the menu:

    Module _highlights is in position 2
  • Browse to https://yourdomain/other and see the menu:

    Module _highlights is in last position

Thanks! I’ll take a look at that.

I’ll do a clean Grav installation to test things out and see if I can implement it on the existing site.

Well it does work on the Grav skeleton.

I can even set the order. However, I do get an error trying to edit the page on the Admin:

An exception has been thrown during the rendering of a template ("Array to string conversion").

As soon I delete the “@page.page” from the items list, I can edit the page just fine. I think the Admin wasn’t made for complex collections.

I don’t know, why it doesn’t work on the site I’m working on. The site it’s weird. It basically uses everything from the frontmatter, not the content of the MD files, just variables in the header and that’s how the info is displayed on the site.

Thoughts?

@el_charlie, It seems you’re right. I can reproduce the issue in Admin when using a complex collection.

I haven’t been able to find a related issue in repos of Grav nor Admin.
You might consider creating an issue, or feature request, at the repo of the Admin plugin.

I don’t know, why it doesn’t work on the site I’m working on.

Without being able to review your site, it’s hard to tell why complex collections wouldn’t work at your site.

The site it’s weird. It basically uses everything from the frontmatter, not the content of the MD files, just variables in the header and that’s how the info is displayed on the site.

No Markdown section in page, but content in variable in frontmatter/header? That sounds weird indeed. I wonder how the pages are being edited and how the template looks like. Also confusing that a modular page uses a ‘default’ template. Not quite Grav-like.