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

Modular page content chunks

Started by Muut Archive 11 years ago · 20 replies · 676 views
11 years ago

I am trying to use modular pages as a means to chunk and reuse content over multiple pages.

For example, in one case I'd like to hide a module page section from being displayed in it's parent but be displayed on another page - is this possible? In another situation I'd like the same module page section to be displayed in it's parent page and as well in another page - is that possible as well?

Thanks very much,
Paul

11 years ago

I think rather than modular data which is quite specific to being included on a single page, you should simply create a folder pages/modules/ and put your modular/chunk/widget content in there.

Then you can include anything on any page with a simple page.find(). There is already an example of this kind of thing in the documentaiton

TWIG
{% include 'modules/module-render.html.twig' with {'page': page.find('/modules/my-module)} %}

Of course you will have to create a simple module-render.html.twig which outputs the {{ page.content() }}

11 years ago

Thanks very much for the helpful info, I'll explore that approach and see how things go.

11 years ago

Starting to explore this approach, and before I too far I was wondering if the same basic idea would be possible without being theme independent?

For example, would this technique still be effective if I included the needed Twig code within the required pages? I am trying not to tie this solution to a specific theme, if possible.

Thank you,
Paul

11 years ago

Well i'm not sure how you could make it completely theme independent. Your talking about rending bits of content in bits of the theme. There is no module/widget position concept in Grav itself, all that is handled by the theme.

Interestingly enough Gantry5 will provide a lot of this infrastructure for Grav allowing you to control layouts and put blocks of content and put Gantry5 Particles into various locations. This is something that is handled by the theme framework layer that sits between Grav and the Theme.

11 years ago

Thanks for the clarifications Andy, I always appreciate all the help you very patiently supply here on the forum!

If I use Grav to create my project, I would want to share the results of my work with other educators so they could use it for their own courses. This is why I was wondering about creating a solution which was not theme-dependent.

Hypothetically speaking, if I proceeded and customized a version of your Bootstrap theme, would it be ok to re-distribute with the proper credit/license?

Thanks,
Paul

11 years ago

Yes, that would be fine. Also I think it would be a great idea to release a Skeleton type package for other educators that includes the theme you develop + sample content and configuration. You could always use the theme inheritance of Grav to provide theme customizations without having to completely copy the entire theme and lose updates etc.

11 years ago

I'd love to contribute a Skeleton type package if I can get things going as I hope!

Speaking of which, I've been able to get some of what you have described so far but I keep getting an error on a page I am including some Twig on to insert some related modular content.

The page content is:

YAML
title: Resources
process:
    twig: true

{% include 'resource-render.html.twig' with {'page': page.find('/resources/topic1)} %}

But I keep getting this error: Unexpected character "'" in "@Page:/Users/paulhibbitts/Dropbox/MAMP/htdocs/grav/user/pages/02.modu le-1/_resources" at line 2

If I swap out twig: true for markdown: true everything works fine.

Any thoughts? I've been clearing the cache as try out things.

Thanks,
Paul

11 years ago

Looks like your missing a ' before the last )

TWIG
{% include ‘resource-render.html.twig’ with {'page': page.find('/resources/topic1)} %}

should be:

TWIG
{% include ‘resource-render.html.twig’ with {'page': page.find('/resources/topic1')} %}
11 years ago

BTW, try and post code blocks between the triple back-ticks like in regular markdown, makes it much easier to read and see obvious missing things.

11 years ago

Well, that was a bit embarrassing :-) Thanks for pointing that out!

I am trying to get content from /resources/topic1 to display elsewhere, but while I do not get an error anymore (thank you!) I get what looks like an empty result.

The page header is now

YAML
title: Resources
process:
    twig: true

Page content

TWIG
{% include 'resource-render.html.twig' with {'page': page.find('/resources/topic1')} %}

For resource-render.html.twig I've got:

TWIG
{{ page.content }}

And in /resources/topic1 I've got page header

TXT
title: Topic1

Page body:

TXT
Resource Topic1

The actual path is "/03.resources/ topic2" but if I understand correctly I do not need to include #'s or 's

Any help is greatly appreciated.

Thanks,
Paul

11 years ago

Well that looks about right. I'm probably going to have to try to debug this scenario to see what's going on.

11 years ago

Thanks for having a look Andy, I appreciate that. This ability of reusable content is a great feature of Grav, but one that I struggled with to get a working example going.

If it helps you to have access to my test site, I can put it up for you.

Thanks,
Paul

11 years ago

I just tested this and it worked 100% fine.

1) I have a plugin i put test templates, but would work the same if you dropped the resource-render.html.twig in the templates/ folder of the theme your using.

2) I created a default.md page with the same header/content you listed above.

3) My resources page i put in a folder user/pages/resources/topic1/default.md

Results displays fine, just as expected.

11 years ago

Thanks for testing it out, I must have something awry somewhere. Would it be possible for you to share this working example somehow? I really don't know why I am getting null on my end, thanks.

11 years ago

Thanks very much Andy, I couldn't get it to work as described but once I dropped the two folders to the page/ folder it worked! I was also able to create a topic2 folder, change the search string, and get the expected results :-)

However, once I tried to do the same technique where the resource topics folders were module components (underline prefix) it once again would result in null results.

Can you duplicate this issue on your end re: using modular folders? i am assuming his same technique would work for either child or module folder content.

Thanks,
Paul

11 years ago

Well like I said modular pages in grav won't work like this. They are considered parts of a page, and won't be found with page.find(). So that behavior is expected.

11 years ago

Oh, I am sorry I must have missed that! However, to be honest I would still expect that behaviour to be supported.

Is there another way to achieve the same results, i.e. re-use content from within a module page section on another page?

Thanks,
Paul

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1318 9 years ago
Archive · by Muut Archive, 9 years ago
2 915 9 years ago
Archive · by Muut Archive, 9 years ago
2 4044 9 years ago
Archive · by Muut Archive, 9 years ago
1 2920 9 years ago
Archive · by Muut Archive, 9 years ago
3 1104 9 years ago