Forum

Thread tagged as: Question

Using page template as a controller

Hi all,

Just wondering if there is a way of using page properties to load different layouts, as a kind of MVC controller.

In WordPress I would do something like this


<?php if(is_page(818)){ // buy a voucher get_template_part('inc/booking/tmpl_booking-form-vouchers'); get_template_part('inc/booking/tmpl_payment-option'); } if(is_page(231)){ // vouchers get_template_part('inc/vouchers/tmpl_vouchers'); } if(is_page(233)){ // groups get_template_part('inc/groups/tmpl_groups'); } if(is_page(235)){ // book get_template_part('inc/booking/tmpl_booking-form'); get_template_part('inc/booking/tmpl_payment-option'); } if(is_page(1409)){ // thank you for booking get_template_part('inc/booking/tmpl_booking-message'); } if(is_page(1488)){ // thank you get_template_part('inc/contact/tmpl_thank-you-message'); } ?>

I've read the threads about using page attributes and I've attempted this, but this just echos out the page ID and doesn't do any processing against it.


$pageID = perch_page_attribute('pageID'); if ($pageID == 1){ echo "we on home page"; };

Any help would be greatly appreciated.

Lee

Lee Goodman

Lee Goodman 0 points

  • 5 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

Use:

$pageID = perch_page_attribute('pageID', true);

Thanks Drew,

Can you explain how this should work. If I try using your example

$pageID = perch_page_attribute('pageID', true);

if ($pageID == 1){
        echo "we are on home page";
    }

This doesn't meet the condition. If I do a var_dump($pageID); it returns a boolean. I am trying to match the ID and load a template part if it is a specific page.

Many thanks

aha I think I have it!


$pageID = perch_page_attribute('pageID', [], true);

as opposed to


$pageID = perch_page_attribute('pageID', true);

seems to work.

Thanks Drew

Drew McLellan

Drew McLellan 2638 points
Perch Support

Probably the better solution is to use Perch Runway, which has routing built in.

Ahhh of course it does, I haven't used that feature yet but a light bulb has just gone off!!