Forum

Thread tagged as: Question, Runway

Confused about master pages and collections

I have 3 pages. Each has a master page, and each has a collection. Each collection is the same data structure, just different data relevant to each page. Each master page is identical to the others, the only difference being the reference to the collection.

In each master page, I use a call like <?php perch_collection('Collection Name', array('template' => 'featurette.html')); ?>

It feels like I should be able to have a single master page, used by all 3 pages, then each page specifies the collection to use, but I'll be damned if I can't figure out how to do this.

I've looked through the documentation, but can't seem to get it straight in my head. Can anyone help?

Chris George

Chris George 0 points

  • 2 years ago

Runway or Perch.... I am guessing Runway.

Sorry, Runway

In Runway you could just use a route and then use...

ROUTE LIKE: page/[*:collection]

<?php perch_collection(perch_get('collection'), array('template' => 'featurette.html')); ?>

REF: https://docs.grabaperch.com/runway/routing/named-segments/

I like that, but if I did this, could I have a user-visible url of say https://mydomain/page1 that routes to https://mydomain/commonmasterpage/mycollection1

I don't want to expose the end user to the underlying data structure of my website any more than I have to.

(sorry, I haven't use custom routing yet... I can try this, but won't be until tomorrow so if you already know that would be helpful! :-)

Thanks Chris

Drew McLellan

Drew McLellan 2638 points
Perch Support

I would use page attributes to select the collection to use on the master template.

Drew McLellan said:

I would use page attributes to select the collection to use on the master template.

Yes, that's a great way too... Likely easier... I just love routes myself so thats where I am most of the time. Not saying it's best (my way)... but it does the trick :)

Thanks Drew and Robert, I'll read up on page attributes and give it a go...

Ok, I've got it working, but want to check something.

By doing <?php perch_collection(perch_page_get_attribute('collection'), array('template' => 'featurette.html')); ?>

This works.

But I can't find perch_page_get_attribute documented anywhere (I only found it by chance on the forum). Is it deprecated? I tried using perch_page_attribute('collection', true), but it just prints the collection string to the page, and doesn't get the actual collection data.

Hi Chris

It's definitely used! There are two other functions associated with it if you look at the docs which could also be handy for you.

I use it to turn on and off features for pages and I reckon you could adapt the same method to achieve what you want:

In your page attributes template you'd have an input to turn on and off the collection (here it's a checkbox but you'd probably need a radio button group) e.g:

<perch:pages id="collectionname_enable" label="Enable Collection A" type="checkbox" checked="checked" value="enabled">

Then in your master page you can check for the value: here an if conditional but will probably be better as a switch for you:

$CollectionActive = perch_page_attribute('collectionname_enable', [], true);
if ($CollectionActive == 'enabled') {
  $CollectionName = 'Marmalade';
};

Then get your collection data (I've added skip template/return-html options so you don't have to make the collection call twice) and assign it to a variable.

$CollectionSelected = perch_collection($CollectionName, [
          'template' => 'featurette.html'.
          'skip-template' => true,
          'return-html' => true,
]);

Then output your Collection template later on in the page:

echo $CollectionSelected['html'];

NB I haven't tested this but think it should get you most of the way!

Hope it helps,

Jon

Drew McLellan

Drew McLellan 2638 points
Perch Support

But I can't find perch_page_get_attribute documented anywhere (I only found it by chance on the forum). Is it deprecated?

It was new in Perch 3.0. It's the same as doing:

perch_page_attribute('collection', [], true)

arrggghhhhh!! I failed to put the empty array in the function call... so that's why it didn't work! But I'll continue using the 'get' version. It's more readable.