Create sections from child pages: template-name is unavailable

Hi there,

This is my second Kirby project and I’m looking into using templates as page sections. I could not decide whether to make this a question or a suggestion, as there is a possible solution already I chose to ask about this first.

For my articles I like to use child pages that act as modules (template files) which I can use over and over.

Structure

  • article (default page)
    – banner
    – text
    – quote
    – text
    – gallery

Etcetera.

Failed attempt
My first attempt was to use a foreach loop and include a snippet with the template method on the current object:

<?php foreach( page()->children()->visible() as $section ):
	
	snippet( $section->template() );
	
endforeach ?>

This however seems to return the $page template value (default) instead of that of $section.

Workaround
When I hardcode the template name it works, for each child page the snippet is included:

<?php foreach( page()->children()->visible() as $section ):
    	
    	snippet( 'text', array('section' => $section) );
    	
endforeach ?>

However, this only works when all templates are the same, which is never the case. The array pushes $section to the template, allowing me to acces it’s contents.

Summary
It seems the template name is lost from the $section object, and it falls back to the $page object. If this would be fixed, multiple, reusable sections on a page would be super-easy. It is a minor but key piece of information that seems to be missing.

As search suggests, more people have been finding ways to get around page sections:

http://forum.getkirby.com/t/solved-how-to-fetch-content-from-subpages-with-different-templates/466
http://forum.getkirby.com/t/add-explicit-support-for-multi-section-pages/822/7

Disclaimer: I did try the sections plug-in, it didn’t seem to do much for me though. (and I can’t post links yet).

Question
Is there any way to retrieve the template name from the child page object as my first attempt suggests? This seems to be by far the easiest solution.

Suggestion
Make sure the template data is available in child page objects and sections become forever amazing*.

*maybe give (section) blueprints an option to redirect to the parent page, preventing linking directly to page sections.

Thank you

I think what you are looking for is

$section->intendedTemplate()
1 Like

Thank you so much, you are an amazing person and life smiles upon you.

Furthermore, let’s direct anyone in need of sections to this:

<?php foreach( page()->children()->visible() as $section ):
		
	snippet( $section->intendedTemplate(), array('section' => $section) );
	
endforeach ?>

This includes al visible children as snippets. I’m pretty confident I’ll use this in just about any future project.

Just to clarify!

Calling the template method only returns the template name if the intendedTemplate matches an actual template file in your templates/ directory. intendedTemplate will always return the value you picked in the Panel (consequently, the name of your content file).

This is a pretty common pattern— used it many times, myself.

1 Like