Page 1 of 1

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

Posted: Fri Jan 09, 2009 12:20 pm
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

Re: How to check if current page has any children

Posted: Fri Jan 09, 2009 12:32 pm
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

Re: How to check if current page has any children

Posted: Fri Jan 09, 2009 12:52 pm
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!

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

Posted: Fri Jan 09, 2009 2:18 pm
by RonnyK
You could check CGExtensions.... IIRC is it having such variables....

Ronny

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

Posted: Thu Jan 22, 2009 10:56 pm
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.