CFMenu Primer

Word Count: 853

One of the great new tags in ColdFusion 8 is the CFMenu tag. This tag allows you to create a menu or toolbar that can run horizontal or vertical and can be styled to your liking. To create a menu you need 2 tags, cfmenu & cfmenuItem. The top level cfmenu tag defines the characteristics of the menu while the cfmenuitem tag will define each of your menus. I am not sure how many nested menus you can create but I have already created a 3 level navigation menu. Lets get started on create the menu. The first thing you need to do is create the menu and set up some basic settings of your menu. Alone the code below will not run because you need at least 1 menu item but the code is syntactically correct. To learn more about all of the attributes please refer to the cfml reference guide.

<cfmenu name="myMenu" bgColor="##CDEB8B" font="Times" fontColor="##73880A" fontSize="12" selectedFontColor="##356AA0" selectedItemColor="##C3D9FF" type="horizontal">
            
</cfmenu>

Now that we have our menu setup we need to start adding menu items. The cfmenuitem tag defines an entry in a menu, including an item that is the head of a submenu. You can nest this tag to create multiple levels of menus. One important note to that though from the cfml refernce guide

If the cfmenuitem tag does not have a body with an end tag, you must close the tag with a forward slash character before the closing greater than character (/>), for example,
<cfmenuitem divider="true" />
At the very least you need to provide a display attribute, this is the text to display on the menu. You can also display an href attribute as a link location or javascript function name.
<cfmenuitem name="home" display="Home" href="##"/>
<cfmenuitem name="products" display="Products" href="##"/>
<cfmenuitem name="services" display="Services" href="##"/>
<cfmenuitem name="support" display="Support" href="##"/>
<cfmenuitem name="Company" display="Company" href="##">

Finally we can put the menu all together. One thing new in ColdFusion 8 is the ability to provide the attribute Collection argument to a tag. I have spoke about this before so run through my archives if you are not sure what this is. I like the syntax better so my final example will create a structure, define the menu properties as that structure and pass it to the menu as an attribute collection. You will also notice that I have nested menus to show you an example of creating sub menus.

<cfset menu = structNew()>
<cfset menu.name = "myMenu">
<cfset menu.bgColor = "##CDEB8B">
<cfset menu.font = "Times">
<cfset menu.fontColor = "##73880A">
<cfset menu.fontSize= 12>
<cfset menu.selectedFontColor = "##356AA0">
<cfset menu.selectedItemColor = "##C3D9FF">
<cfset menu.type = "horizontal">
<!--- you can not specify a width if the menu type is horizontal --->
<cfif menu.type EQ "vertical">
<cfset menu.width = "100">
</cfif>

<cfmenu attributeCollection="#menu#">
<cfmenuitem name="home" display="Home" href="##"/>
<cfmenuitem name="products" display="Products" href="##"/>
<cfmenuitem name="services" display="Services" href="##"/>
<cfmenuitem name="support" display="Support" href="##"/>
<cfmenuitem name="Company" display="Company" href="##">
<cfmenuitem name="about" display="About Us"/>
<cfmenuitem name="news" display="News"/>
<cfmenuitem name="jobs" display="Jobs"/>
<cfmenuitem name="articles" display="Articles">
<cfmenuitem name="current" display="Current Articles"/>
               <cfmenuitem name="archive" display="Articles"/>
</cfmenuitem>
</cfmenuitem>
      <cfmenuitem name="store" display="Store">
         <cfmenuitem name="shopnow" display="Shop Now"/>
         <cfmenuitem name="account" display="Your Account"/>
      </cfmenuitem>
</cfmenu>

If you have any questions feel free to leave a comment. If you would like to see this menu in action just click the link below. Also be on the look out for a couple more menu examples.
http://h127408.cf8beta.com/cfmenu/index.cfm

Comments

#1 Posted By: Adam Fortuna Posted On: 6/1/07 10:50 AM
Very cool man! I have to ask though; what are you using to format the cfml displayed on that pages nicely?
#2 Posted By: Dan Posted On: 6/1/07 10:53 AM |
Author Comment
Adam,
Thanks! I actually just put the code into my blog entry and did a preview, from there I was able to copy the formatted code. If you use BlogCFC you should be able to do the same.
#3 Posted By: Gary Fenton Posted On: 6/1/07 11:34 AM
Do you realise that demo page contains 243Kb of external javascript files? While it's very nice to be able to set up a menu in a dozen lines of code I think the download size outweighs that benefit. Perhaps CF can be told to get the browser to download the relevant js files only?
#4 Posted By: Michael White Posted On: 6/1/07 1:28 PM
Is flash form style menus supported?
#5 Posted By: Dan Posted On: 6/1/07 1:44 PM |
Author Comment
Michael,
The docs do not say anything about that but because the cfml reference does not include any format attributes my guess is no. It seems that its just css/html. You may want to ping the higher ups though.
#6 Posted By: tony Petruzzi Posted On: 6/1/07 2:22 PM
@Gary,
this is why I think that I stick with jQuery and jdMenu for all my menus.
#7 Posted By: Damon Cooper Posted On: 6/1/07 6:30 PM
@Tony...this is a Beta...only a Beta :)
#8 Posted By: Mark CE Posted On: 6/19/07 9:11 AM
anyone know how to indicate that a menuitem is selected?
#9 Posted By: Scott Bennett Posted On: 8/9/07 7:19 PM
I have been playing with the cfmenu today and noticed that in IE7 it always flashes the default selected bgcolor before it switches to the color I specify in the selecteditemcolor attribute on the cfmenu tag. It works fine in firefox of course but I was wondering if you were aware of this and/or if you can figure out a way to fix it.

I'm thinking maybe I can modify the /CFIDE/scripts/ajax/resources/yui/yui.css file but haven't looked into that too much yet.
#10 Posted By: James Papaleo Posted On: 11/13/07 8:35 AM
Very nice but if you would like to see a dynamic menu created from a database, check out my example at www.learncf.com.


Post Your Comment

Leave this field empty







Show Captcha

If you subscribe, any new posts to this thread will be sent to your email address.

Copyright © 2007 Dan Vega | BlogCFC was created by Raymond Camden. This blog is running version 5.8.001.