Show children of current page unless hidden from the menu

The place to talk about things that are related to CMS Made simple, but don't fit anywhere else.
Post Reply
gocreative
Power Poster
Power Poster
Posts: 265
Joined: Mon Mar 14, 2011 1:16 am

Show children of current page unless hidden from the menu

Post by gocreative »

I'm using a UDT for CMS 1.11 called list_children to display the children of the page currently being viewed. If there is at least one child page, I wrap a HTML <section> element around the list. That all works fine.

Now for the tricky bit. I only want to list pages that are set to appear in the menu. Again, I can achieve this, but the problem is that the outer HTML elements (<ul> and <section>) will be displayed regardless.

So, what needs to happen is this logical process:

1. Are there child pages of the current page? If yes, continue.
2. Is there at least one page that is set to show in the menu? If yes, continue.
3. Is this showing in the sidebar (using the parameter when calling the UTD)? If yes, wrap the whole block in <section>.
4. Wrap the list items in the <ul> element.
5. Loop through the child pages, ignoring those that that NOT set to show in the menu. Output each to an <li>.
6. Close the </ul> and </section>.

Here's what I have so far. This is completely functional except that it shows the outer HTML elements regardless.

Code: Select all

global $gCms;

// invoke menu manager
$manager =& $gCms->GetHierarchyManager();

// get the current page name
$thisPage = $gCms->variables['page_name'];

// define a specific page alias or use the current page
if (isset($params['the_page_alias'])) {
	$currentNode = &$manager->sureGetNodeByAlias($params['the_page_alias']);
}
else {
	$currentNode = &$manager->sureGetNodeByAlias($thisPage);
}

// get the list of child pages
$nodes = $currentNode->getChildren();

// if there are children...
if ( $currentNode->hasChildren() ) {

	// is this showing in the sidebar?
	if (isset($params['sidebar'])) {
		if ($params['sidebar'] == 'yes') {
			echo '<section id="subpages">';
			$sidebar_end = '</section>';
		}
	}

	// redefine or hide the default list title
	if (isset($params['title'])) {
		if ($params['title'] !== '') {
			echo '<h3>'.$params['title'].'</h3>';
		}
		else { /* do nothing */ }
	}
	else {
		echo '<h3>Pages in this section:</h3>';
	}

	// set a class name for the list
	if (isset($params['class'])) {
		if ($params['class'] !== '') {
			echo '<ul class="'.$params['class'].'">';
		}
		else { /* do nothing */ }
	}
	else {
		echo '<ul>';
	}

	// show each item in a list format
	foreach ($nodes as $node) {
	
		$content= $node->getContent();
		
		$url = $content->GetURL();
		
		// hide section header pages
		if ($url == "#") {
			$url = "index.php?page=".$content->Alias();
		}
		echo "<li><a href=\"".$url."\">".$content->MenuText()."</a> </li>";

	}

	echo "</ul>";

// if it's in the sidebar, close the element
echo $sidebar_end;

}
Jos
Support Guru
Support Guru
Posts: 4020
Joined: Wed Sep 05, 2007 8:03 pm

Re: Show children of current page unless hidden from the men

Post by Jos »

you might want to check the available parameters of the menu tag in the module help of menumanager.
My guess is that you don't need a udt at all
gocreative
Power Poster
Power Poster
Posts: 265
Joined: Mon Mar 14, 2011 1:16 am

Re: Show children of current page unless hidden from the men

Post by gocreative »

You may be right, but I'm using this UDT in a few different places so I need to specify its title, CSS class, location and other factors (truncated from this code). I'm not sure how I could achieve all of this with menu manager.
uniqu3

Re: Show children of current page unless hidden from the men

Post by uniqu3 »

Like Jos said i don't see a reason fo a UDT either, for you sidebar section or classes all you need is additional content block which is proccessed before {menu}

so somewhere on top of template you would do:

Code: Select all

{content block='is_sidebar' assign='is_sidebar' label='Enable Sidebar' size='20' oneline='true'}
{content block='i_want_class' assign='i_want_class' label='Enter Class name' oneline='true'}
{content block='from_alias' assign='from_alias' label='Specify page alias for menu to show' oneline='true'} 
then in menu

Code: Select all

{if !empty($is_sidebar)}
  <section id="subpages">
{/if}
and so and on the rest of template
{if !empty($is_sidebar)}
  </section>
{/if}
and in page template where menu is called

Code: Select all

{if !empty($from_alias)}
  {menu childrenof=$from_alias .. and other params}
{else}
  {menu start_level='2' .. or whatever should happen by default}
{/if}
At least from what i understand you want to achieve, but anyway all you need is proper logic to come to your goal.
gocreative
Power Poster
Power Poster
Posts: 265
Joined: Mon Mar 14, 2011 1:16 am

Re: Show children of current page unless hidden from the men

Post by gocreative »

Thanks mate for the reply. That's not quite what I'm after, though. Looking at your code, the sidebar section is going to be shown even if there are no menu items to display.

I also don't want to edit it per-page. I just want to drop this into the sidebar of the page template and have it automatically use the current page alias, so I don't need any of those content block settings.

And lastly, even if I moved <section> to within the menu template, how would I be able to specify a class for that element for each new instance of the menu?
uniqu3

Re: Show children of current page unless hidden from the men

Post by uniqu3 »

Without actually knowing what you are after cause it all doesn't make sense to me :) but trying to answer hopefully correct.

If my guess is correct you want to show sidebar menu if page has children and on all children pages of that page.
This can be easily solved with CGSimpleSmarty.
Usually i do all of the logic on top of tmeplate so you would do:

Code: Select all

{* get parent alias from current page *}
{$cgsimple->get_parent_alias($page_alias, 'has_parent')}
{*do the logic *}
{capture assign='is_parent'}{if !empty($has_parent)}{$has_parent}{else}{$page_alias}{/capture}
{$cgsimple->get_page_title($is_parent, 'has_title')} 
Then in MenuManager template

Code: Select all

<section class='{$is_parent}'>
<h3>{$has_title}</h3>

the menu stuff

</section>
Then where you call your sidebar menu

Code: Select all

{menu childrenof=$is_parent}
Bus basically to hide inactive or hidden pages in you udt add following where you have:

Code: Select all

// show each item in a list format
   foreach ($nodes as $node) {
   
      $content= $node->getContent();
      // add this here
      if(!$content->Active() || !$content->ShowInMenu()) continue;
gocreative
Power Poster
Power Poster
Posts: 265
Joined: Mon Mar 14, 2011 1:16 am

Re: Show children of current page unless hidden from the men

Post by gocreative »

I think that might work, but I'll have to try it tomorrow. For clarity, here's what I'm trying to do:

* Have a single function/call (e.g. a UDT) which allows me to show a list of all child pages of either the current page, or a specific page alias.

* I want the function to be flexible so that I can use it in multiple websites. Sometimes it will appear in the sidebar (which is why I'm trying to assign a class to the UL), but other times it might appear in the general page content area or somewhere else.

* I don't want to have to manually exclude any page aliases (which is great in your solution -- I'm just clarifying that).

* The whole reason I need this is because I sometimes have a 'Thank you' page which is only used as a confirmation page after a Form Builder form is submitted. I want the client to be able to edit this page and I don't want to involve any GCBs or anything else, so that needs to be a normal page. Obviously I don't show it in the menu and I also don't want it to appear in the sidebar. There may be other such pages so I'm just filtering out all pages that aren't set to show in the menu, to avoid that issue.

* For this particular website, I want to show the block in the sidebar, so I need to firstly assign the class "sidebar" to the UL, but I also need to skip the hidden pages (as per the point above).

As I said, I'll have to test out your code but this is the conceptual logic:

1. If the current page has children and there's at least one page that is set to show in the menu, list all of those pages. If not, stop here.
2. Allow me to assign a Class or ID to the <section> that wraps around the <UL>.

That's pretty much it.
Post Reply

Return to “The Lounge”