Proper way to use AutoNav to create separate navigation regions
Permalink
I'd like to embed two AutoNav blocks on every page of my site. The main (top-level) navigation across the top and the relevant sub-page navigation (when appropriate) in the sidebar.
I've installed the Nine Sixty Starter theme (a website without design) to work out the technique I should use. I have the pages all defined, and I'm using aliases for some pages so they appear within subpages as "secondary" navigation, but when the links are exercised the context moves to the top-level page they reference and then I want that page's sub-page navigation to appear in the sidebar.
The problem I'm having is getting the sub-page navigation properly instanced and presented. I can't seem to find a decent reference to the configuration parameters of the AutoNav block (which I really want to use versus rolling my own) and I'm hopeful it can be done.
Here's a link to the site under development:http://neair.danaadams.org/ (you can see the alias behavior working by clicking on "Architect" in the top-level nab and then "Mechanical Construction Services" under "Architect" in the sub-nav.
But I just don't know how to configure the sub-nav to show only sub-pages and below for the current top-level nav...
Here's the code I put in the default.php file of the theme...
Any ideas??
I've installed the Nine Sixty Starter theme (a website without design) to work out the technique I should use. I have the pages all defined, and I'm using aliases for some pages so they appear within subpages as "secondary" navigation, but when the links are exercised the context moves to the top-level page they reference and then I want that page's sub-page navigation to appear in the sidebar.
The problem I'm having is getting the sub-page navigation properly instanced and presented. I can't seem to find a decent reference to the configuration parameters of the AutoNav block (which I really want to use versus rolling my own) and I'm hopeful it can be done.
Here's a link to the site under development:http://neair.danaadams.org/ (you can see the alias behavior working by clicking on "Architect" in the top-level nab and then "Mechanical Construction Services" under "Architect" in the sub-nav.
But I just don't know how to configure the sub-nav to show only sub-pages and below for the current top-level nav...
Here's the code I put in the default.php file of the theme...
<?php defined('C5_EXECUTE') or die(_("Access Denied.")); $this->inc('elements/header.php'); // get header file ?> </head> <body id="backpage"> <div class="container_12"> <h1><a href="<?php echo DIR_REL?>/"> <?php $block = Block::getByName('My_Site_Name'); if( $block && $block->bID ) $block->display(); else echo SITE; // display site title ?></a> </h1> <div class="main_nav">
Viewing 15 lines of 47 lines. View entire code block.
Any ideas??
@mkly - thank you for taking the time to look at this, your suggestion got me a lot closer.
I took your code suggestion and updated my $bt_sub object as follows:
I have to instantiate a new object for the sub nav because I can't re-use the main nab object - wacky results if I try.
And, it works perfectly for 99% of the use cases. However, when on the 'Home' page, I get a duplicate of the main navigation because 'Home' is one of those tricky parent-peer navigation items.
I need to find a way of 'knowing' I'm on the 'Home' page and not display the sub nav. I suppose I could wrap the code in an if statement, but what would I look for? Is the ID of the home page always 0?
I took your code suggestion and updated my $bt_sub object as follows:
$bt_sub = BlockType::getByHandle('autonav'); $bt_sub->controller->mainCollection = $c; $bt_sub->controller->displayPages = 'below'; $bt_sub->controller->orderBy = 'display_asc'; $bt_sub->controller->displaySubPages = 'all'; $bt_sub->controller->displaySubPageLevels = 'enough'; $bt_sub->render('view');
I have to instantiate a new object for the sub nav because I can't re-use the main nab object - wacky results if I try.
And, it works perfectly for 99% of the use cases. However, when on the 'Home' page, I get a duplicate of the main navigation because 'Home' is one of those tricky parent-peer navigation items.
I need to find a way of 'knowing' I'm on the 'Home' page and not display the sub nav. I suppose I could wrap the code in an if statement, but what would I look for? Is the ID of the home page always 0?
I think there is a constant HOME_CID that you can use to tell if it's the home page.
should get you there.
$pg = Page::getCurrentPage(); if($pg->getCollectionID() != HOME_CID) { // do stuff }
should get you there.
That works, but when I get to the bottom page the "peer" sub nav disappeared. I hacked a solution that will work for my purposes but I'm not sure it will work if there's yet another page level below 2-tiers.
What I did was to check if the current page has any children, if not I set displayPages to current. As I said it works for my purposes, but it may not be the best way to accomplish this...
What I did was to check if the current page has any children, if not I set displayPages to current. As I said it works for my purposes, but it may not be the best way to accomplish this...
$pg = Page::getCurrentPage(); if($pg->getCollectionID() != HOME_CID) { $bt_sub = BlockType::getByHandle('autonav'); $bt_sub->controller->mainCollection = $c; if($bt_sub->controller->mainCollection->cChildren==0) { $bt_sub->controller->displayPages = 'current'; $bt_sub->controller->orderBy = 'display_asc'; $bt_sub->controller->displaySubPages = 'all'; $bt_sub->controller->displaySubPageLevels = 'enough'; } else { $bt_sub->controller->displayPages = 'below'; $bt_sub->controller->orderBy = 'display_asc'; $bt_sub->controller->displaySubPages = 'all'; $bt_sub->controller->displaySubPageLevels = 'enough'; }
Viewing 15 lines of 17 lines. View entire code block.
At this point it's probably easier to create a new "block template" for the autonav. I'm not sure if you have done this before, but it gives you a whole lot more flexibility. There is a really good autonav starter template that is very well commented at
https://github.com/jordanlev/c5_clean_block_templates...
You would rename the view.php file to something like my_new_template.php and place in in the root at
Then replace your render method with
Once you start getting pretty custom with your autonav it's usually just easier to create a custom template.
But it what you have works now, I would probably just leave well enough alone.
https://github.com/jordanlev/c5_clean_block_templates...
You would rename the view.php file to something like my_new_template.php and place in in the root at
/blocks/autonav/templates/my_new_template.php
Then replace your render method with
$bt_sub->render('templates/my_new_template');
Once you start getting pretty custom with your autonav it's usually just easier to create a custom template.
But it what you have works now, I would probably just leave well enough alone.
EDIT: Ok maybe this is what you are looking for?
Please don't take this as a jerky comment. The autonav controller.php file actually helps quite a bit if you look at the switch statements etc. You can kind of feel out how the options on the form map to the variables with that.