[SOLVED] How to show autonav block if in a section?

Permalink 2 users found helpful
Hi,

Trying to cut down on the number of page templates I'm creating for my clients, so was thinking I could hardcode an autonav depending on the section Im in.

For example, with the following sitemap, I need Section 1 and *all* it's subpages (including S1 2a and S1 2b) to show Autonav A, Section 2 and all its subpages to show Autonav B etc.:

Section 1
-S1 p1
-S1 p2
--S1 p2a
--S1 p2b
-S1 p3
Section 2
-S2 p1
-S2 p2
-S2 p3
Section 3
-S3 p1
-S3 p2
-S3 p3
--S3 p3a
--S3 p3b

Does anyone know how to do that please?

Thanks

osu

osu
 
ScottC replied on at Permalink Best Answer Reply
ScottC
$v = View::getInstance();
$inSection($v->section('Section1'){
//your block knows it is in section1 of the site
}


Then depending on how you want to do it from there, be it via CollectionChildrenArray or a hardcoded autonav block, i'll leave that up to you :)
osu replied on at Permalink Reply
osu
That's great Scott, thanks for this. Out of interest, how would I go about the CollectionChildrenArray approach? I've always put an autonav in the global blocks scrapbook and hardcoded it that way...

osu
ScottC replied on at Permalink Reply
ScottC
well there are a couple ways, basically you would get the pages children array, and it would give you every page underneath it, but not in a heirarchy, just a flat array. so you would actually need recursive checking all the way down(the same way that the autonav template or superfish works).

So what you could also do is do something like $childrenOfHome = Page::getByID(HOME_CID)->getCollectionChildrenArray(true);
Load up the path back to home, which is under NvagiationHelper::getTrailToCollection(HOME_CID);

SO then you could do if each isn't empty then wherever they intersect(http://php.net/manual/en/function.array-intersect.php) in your nav template off that children array of 1(if you only want the first nav elements to be highlighted as active, or if you did getCollectionChildrenArray(false) then you have all of them, so then you could output an "active" li class or something like that.

G'Luck! :)
zoinks replied on at Permalink Reply
ScottC,

I don't really understand how your code works. How does it know it's in "Section1"? Because that's the name of the page?

I want to do the same thing as OP for a parent page named "The Way It Works" to display children subpages ONLY when on this page.

The Way It Works
> Child 1
> Child 2

What code do I need to add to my template?