Forum

Thread tagged as: Question, Runway

Check if Region exists

Hi Is it possible using PHP in a master page template to check if a Region exists and if not load another?

eg.

if(isset(Landing Page) {
$pageContent = perch_content_custom('Landing Page', $opts);
} else {
$pageContent = perch_content_custom('Subpage', $opts);
}

I know this is not correct syntax but wanted to give a better idea of what I am trying.

Thanks.

Keith Winter

Keith Winter 0 points

  • 4 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

$pageContent = perch_content_custom('Landing Page', [
    'return-html' => true,
    'skip-template' => true,
]);

if (isset($pageContent[0])) {
    echo $pageContent['html'];
} else {
    perch_content_custom('Subpage', $opts);
}

Thanks Drew

Can't quite get that to fit in with the way I'm displaying content sorry.

I am looping through navgroup assigned pages to show content on home page

Some pages have 'Landing Page' region and some 'Subpage' region.

Here is more of my code -

$navigation = perch_pages_navigation(array(
                    'navgroup' => 'show-on-home',
                    'skip-template' => true
                ));

                if (!empty($navigation)) {
                    foreach( $navigation as $item ) { 
                        $opts = array(
                            'page' => $item['pagePath'], 
                            'skip-template' => true,
                        );

                        $pageContent = perch_content_custom('Landing Page', $opts);

                        if (!empty($pageContent)) {
                                    //set content vars
                                    $thelink = $item['pagePath'];
                                    $theheading = $pageContent[0]['heading'];
                                    $thetext = $pageContent[0]['panel-text'];

                                    // and so on...


I echo out the content for each iteration of the loop.

Hope that explains.

Thanks

Drew McLellan

Drew McLellan 2638 points
Perch Support

Ok, well it should be easy to adapt the principle to fit your page.

Hi Drew Thanks as always -

Got it working except getting I'm getting 'Undefined offset: 0 in.....' error message when loop first encounters the following line and the region 'Page' does not exist.

$pageContent = perch_content_custom('Page', $opts);

The error is displayed and then the isset($pageContent[0]['heading']) function works and the differently named region is used as wanted. In other words the page principle perfectly, using the 2 different pages that have different named regions but displays the error in the middle, I assume because the array is not returned by the perch content custom function?

Here is my code now with the error checking etc


foreach( $navigation as $item ) { $opts = array( 'page' => $item['pagePath'], 'skip-template' => true, ); // error I think is here when perch function first fails to find 'Page' region? $pageContent = perch_content_custom('Page', $opts); if (isset($pageContent[0]['heading'])) { $pageContent = perch_content_custom('Page', $opts); } else { $pageContent = perch_content_custom('Overlay Subpage', $opts); } if (!empty($pageContent)) { //display content here

Thanks again for the help.

Drew McLellan

Drew McLellan 2638 points
Perch Support

This is really just a PHP question.

if (isset($pageContent[0]) && isset($pageContent[0]['heading']))

Thanks Drew. Of course, now I see it, it is obvious. Works like a dream, a beer I owe you! cheers