Only show Divs around hardcoded Autonav if not empty
Permalink
Hi guys,
I'm trying to hardcode autonav in my template.
My code looks like this:
Now I want this navigation to be wrapped by two DIVs ONLY if there are SubPages.
If yes it should output <div id='no1'><div id='no2'>Here the autonav</div></div>.
If no = no output at all.
All help is highly appreciated.
Thank u very much
Toby
I'm trying to hardcode autonav in my template.
My code looks like this:
<?php $bt_main = BlockType::getByHandle('autonav'); $bt_main->controller->displayPages = 'second_level'; $bt_main->controller->orderBy = 'display_asc'; $bt_main->controller->displaySubPages = 'relevant'; $bt_main->render('view'); ?>
Now I want this navigation to be wrapped by two DIVs ONLY if there are SubPages.
If yes it should output <div id='no1'><div id='no2'>Here the autonav</div></div>.
If no = no output at all.
All help is highly appreciated.
Thank u very much
Toby
OR maybe someone knows how to get the number of SubPages in PHP?
There must be a way in php of how to check if a site has subPages!
Okay, I just looked at the old (and, in my opinion a easier to read and modify…) autonav code and came up with something that might work out for you:
You can make more elaborate checks like:
$c = Page::getCurrentPage(); if(is_object($c->getFirstChild())){ //page has at least one child }
You can make more elaborate checks like:
$c = Page::getCurrentPage(); $subpage = $c->getFirstChild; if($subpage instanceof Page){ //your code here }
Thank you very much.
That's enough of a solution I can work with, although first attempt also fires for "home" (as of course in autonav every page is a child of home).
That's enough of a solution I can work with, although first attempt also fires for "home" (as of course in autonav every page is a child of home).
You can check that, too. Just check against the ID of your home page. Or check if the page has any parent. If it doesn't have any, it's the home page.
MY FINAL SOLUTION (in case someone is interested):
global $c; $c = Page::getCurrentPage(); $intChildPages = $c->getNumChildren(); if( ($intChildPages > 0 OR $c->getCollectionParentID()!=1) AND $c->getCollectionID()!=1){ // Will only output if there are subpages (other than home) OR is a subPage itself }
Thanks a lot - Just the code i needed :-)
Thanks alot for this code. Oddly enough, it doesn't work as you've posted. I'm using c5.6.1.2. However, it works perfectly like this:
I'm successfully using this in a custom template page list that includes an auto-nav. I check if the page list item has children, if so, spit out the custom template auto-nav for them, wrapped up in custom presentation code as needed.
Pretty sweet! Hope this helps someone!
global $c; $c = Page::getCurrentPage(); #$intChildPages = $c->getNumChildren();//doesn't work properly $intChildPages = $page->getNumChildren();//this one returns the correct number of child pages if( ($intChildPages > 0 && $c->getCollectionParentID()!=1) && $c->getCollectionID()!=1) { // Will only output if there are subpages (other than home) OR is a subPage itself ...Code here }
I'm successfully using this in a custom template page list that includes an auto-nav. I check if the page list item has children, if so, spit out the custom template auto-nav for them, wrapped up in custom presentation code as needed.
Pretty sweet! Hope this helps someone!