We're working on a site for a client whereby we want all of the root level menu items to be equal width. The problem is, we don't want the menu to break if the client adds or removes items, and we're using a complicated menu which makes it hard to make this happen with CSS.
Is there a way in menu manager to get the total number of root menu items (only those set to appear in the menu) and return this variable for use in a stylesheet?
Automatic equal widths for root level menu items
-
- Power Poster
- Posts: 265
- Joined: Mon Mar 14, 2011 1:16 am
- Location: Brisbane, Australia
Re: Automatic equal widths for root level menu items
You could use a extra menu call with number_of_levels parameter and a custom template and use that to create some inline css.
Somewhere in the <head> section of your page template(s):
Menu template 'output_css'
Obviously '1024' should be replaced with some value that applies to your design.
You could also use one menu call and customize the Menu Manager template to count all 'nodes' with property depth == 0 (or 1, I don't remember). Then you can use that count (stored in Smarty variable) in <head> section of page template.
HOWEVER: I think (not sure) there is a bug in Menu Manager that makes depth property unusable. So take care if you try that approach.
Somewhere in the <head> section of your page template(s):
Code: Select all
{menu number_of_levels=1 template="output_css"}
Code: Select all
{if $count > 0}
<style type="text/css">
ul#mainmenu > li {
width : {ceil(1024/$count)}px;
}
</style>
{/if}
You could also use one menu call and customize the Menu Manager template to count all 'nodes' with property depth == 0 (or 1, I don't remember). Then you can use that count (stored in Smarty variable) in <head> section of page template.
HOWEVER: I think (not sure) there is a bug in Menu Manager that makes depth property unusable. So take care if you try that approach.
-
- Power Poster
- Posts: 265
- Joined: Mon Mar 14, 2011 1:16 am
- Location: Brisbane, Australia
Re: Automatic equal widths for root level menu items
Great idea, and so simple! Thanks so much. Here's what I ended up going with, because the site has a couple of responsive breakpoints:
Code: Select all
{if $count > 0}
<style type="text/css">
.top-bar-section > ul.left > li {
width: {math equation="y / x" x=$count y='100'}%;
}
</style>
{/if}
Re: Automatic equal widths for root level menu items
This is how I did similar menu in one of my sites.
I have UDT (count_root_items) with code :
And then in top of menu-template is this:
That "columnwidth" is a class name which is defined in styles. For example width for elements with class "two" is 50% and with "four" it's 25%. You can also add for example different paddings when there is two or six root level items.
I have UDT (count_root_items) with code :
Code: Select all
foreach($params['list'] as $item){
if($item->depth == 1)
$i++;
}
return $i;
Code: Select all
{assign "root_items" {count_root_items list=$nodelist}}
{if $root_items == 8 }
{assign "columnwidth" "onehalf"}
{elseif $root_items == 6}
{assign "columnwidth" "two"}
{elseif $root_items == 4}
{assign "columnwidth" "three"}
{elseif $root_items == 3}
{assign "columnwidth" "four"}
{elseif $root_items == 2}
{assign "columnwidth" "six"}
{elseif $root_items == 1}
{assign "columnwidth" "twelve"}
{/if}