[SOLVED] How to check if current page has any children

For discussion and questions related to CMS Specific templates and stylesheets (CSS), and themes. or layout issues. This is not a place for generic "I don't know CSS issues"
Post Reply
Sphinx
Forum Members
Forum Members
Posts: 12
Joined: Wed Nov 05, 2008 11:39 am

[SOLVED] How to check if current page has any children

Post by Sphinx »

Hi

I can't figure out how I determine if the current page has any childrens.
I am trying to make a conditional that shows a submenu only if there are childrens.

conceptually something like this in my page template:

Code: Select all

{if $haschildren == true}
<ul><li>{menu template='simple_navigation' start_level='2' collapse='1'}</li></ul>
{else}
some other content here
{/if}
but I can't figure out where I get a variable like the fictive $haschildren from.

I can't simply handle this in the menu template as this is not called at all in cases where there are not children..

Any help appreciated!

/Michael
Last edited by Sphinx on Fri Jan 09, 2009 12:52 pm, edited 1 time in total.
RonnyK
Support Guru
Support Guru
Posts: 4962
Joined: Wed Oct 25, 2006 8:29 pm
Location: Raalte, the Netherlands

Re: How to check if current page has any children

Post by RonnyK »

Check this post... It might help...

http://forum.cmsmadesimple.org/index.ph ... #msg138417

IIRC is there some logic in CGExtensions as well, to get some add.info on pages...

Ronny
Sphinx
Forum Members
Forum Members
Posts: 12
Joined: Wed Nov 05, 2008 11:39 am

Re: How to check if current page has any children

Post by Sphinx »

Thanks..

Ok, it seems I can't avoid switching to inline PHP in my template code then. Here's a short test and it works:

Code: Select all

{php}

global $gCms;
$manager =& $gCms->GetHierarchyManager();
$thisPage = $gCms->variables['page_name'];
$currentNode = &$manager->sureGetNodeByAlias($thisPage);

$nodes = $currentNode->getChildren();

if ($currentNode->hasChildren()) {echo "SUBMENU";}else{echo "NO SUBMENU";}

{/php}
I had hoped for a solution via already assigned variables, but this is fine. Thanks!
RonnyK
Support Guru
Support Guru
Posts: 4962
Joined: Wed Oct 25, 2006 8:29 pm
Location: Raalte, the Netherlands

Re: [SOLVED] How to check if current page has any children

Post by RonnyK »

You could check CGExtensions.... IIRC is it having such variables....

Ronny
neis
Forum Members
Forum Members
Posts: 11
Joined: Thu Aug 16, 2007 6:34 pm
Location: Springfield, Vermont, USA

Re: [SOLVED] How to check if current page has any children

Post by neis »

After several trials of my own, I stumbled upon your code. Thank you for this solution.

Since I don't think an inline php is good either, I took your code and made it into a User Defined Tag called get_page_haschildren

Code: Select all

global $gCms;
$manager =& $gCms->GetHierarchyManager();
$thisPage = $gCms->variables['page_name'];
$currentNode = &$manager->sureGetNodeByAlias($thisPage);

$nodes = $currentNode->getChildren();
$ph = 0;
if ($currentNode->hasChildren()) {$ph = 1;}

$var = 'page_haschildren';
$smarty->assign($var,$ph);
I used a trick I learned previously -> how to assign a value to a smarty variable inside a UDT.

In my template I use the following code:

Code: Select all

{get_page_haschildren}
{if $page_haschildren}
  <div id="page-menu">{menu start_page=$page_alias number_of_levels="2" template="pagemenu"}</div>
{/if}
The call to get_page_haschildren can be done anywhere as long as it is done before the use of the $page_haschildren smarty variable.

The call to the menu module is just what I want to display if the page has children but one can do anything else instead.

Thanks again.
Post Reply

Return to “Layout and Design (CSS & HTML)”