Snippet page type structure, with controllers

I think it’s really interesting with different structure solutions like Custom snippets for specific page types.

I tried a new approach this week that I’ll share.

Structure

Let’s say we want to have this snippet structure:

Template 1 structure

sidebar
  news
  ratings
main
  content1
    about
  content2
  table1

Template 2 structure

sidebar
  ratings
main
  content2
  content1
    info
    about
    data

We want to have different templates but with the same snippets depending on the template. This can be done in many different ways.

Before I used if statements like this:

<?php if( $page->intendedTemplate('template1') ) : ?>
  <?php snippet('news'); ?>
<?php endif; ?>

This is just one if statement. I’m not going to write them all here.

Pitfalls

  • It can be really messy with a more complex structure.
  • If need to change the order of the snippets, it will add another layer of complexity.

New approach - Controllers

Template 1 controller

<?php
return function($site, $pages, $page) {
  return array(
    'sidebar' => array('news', 'ratings'),
    'main' => array('content1', 'content2', 'table1'),
    'content1' => array('about'),
  );
};

Template 2 controller

<?php
return function($site, $pages, $page) {
  return array(
    'sidebar' => array('ratings'),
    'main' => array('content2', 'content1'),
    'content1' => array('info', 'about', 'data'),
  );
};

Snippets

A loop will take every value from the array and load snippets from that.

Main snippet

foreach( $main as $item ) {
  snippet( $item );
}

//Template 1 - content1, content2, table1
//Template 2 - content2, content1

Content1 snippet

foreach( $content1 as $item ) {
  snippet( $item );
}

//Template 1 - about
//Template 2 - info, about, data

Benefits

  • Separates the conditions from the templates/snippets.
  • Less code in the templates/snippets.
  • Not much code in the controllers.
  • Order snippets is really simple.
  • Unlimited depth without hassle.
  • Great overview in the controllers.

Sidenotes

Maybe you don’t like to just drop the snippet names into the controller like that.

You can nest it if you like:

It will require an extra step in the controller and in the template, but still much less code than if statements.

<?php
return function($site, $pages, $page) {
  return array(
    'snippets' => array(
      'sidebar' => array('ratings'),
      'main' => array('content2', 'content1'),
      'content1' => array('info', 'about', 'data'),
    ),
    'some_other_thing' => $page->title(),
  );
};

So, what do you think? How do you do it?

That’s an interesting approach and I tried something similar yesterday :slight_smile:

This brings me to a question:
Could this be done to blueprint snippets?

To use in a panel field or in the templates/snippets?

To use in the blueprints. Do you see what I mean?

Ahh, I see. Interesting idea. I wrote something similar here:

1 Like

Yes, that! I knew there had been some suggestions about it somewhere!