Layout Components 1.0 Documentation: Custom Methods

Layout Components Information

Download from RIA Forge

Blog Entries

Custom Methods

Sometimes the built-in methods are insufficient for a given site. You may need to call aspects of a layout from within a page, for example, or you might want to set content outside of the main content area from a page.

For these kinds of situations, you would want to create a custom method. A custom method must first be added to layout.cfc (otherwise you will get an error when you use include a file using Layout Components). If you are creating an output method, then the version of the method in layout.cfc should output nothing (just like the existing "head","body", and "end" methods). If you are adding data to the Layout Component to be output later, however, it is better to have that code in layout.cfc directly.

Here are examples of each kind of custom method:

Output Method

If you need to output a menu within the contents of the page, you could add a custom "menu" method. First add it as a blank method in layout.cfc:

<cffunction name="menu" access="public" output="yes"></cffunction>

In this example, the method doesn't take any arguments but it could be written to use arguments if needed.

Once this is done, add a "menu" method in your main layout components ("Default.cfc", for example) that will output the menu as you want it to display.

Data Method

If you want to be able to add images into a side bar you can add a method into "layout.cfc" to collect that data:

<cffunction name="addSideBarImage" access="public" returntype="any" output="no">
	<cfargument name="src" type="string" required="true">
	<cfargument name="alt" type="string" default="">
	<cfif NOT StructKeyExists(,"aSideBarImages")>
		<cfset = ArrayNew(1)>
	<cfset ArrayAppend(,arguments)>

Make sure to add any data into the "" structure or else the data will be lost whenever you switch layouts.

You can use the data by simply checking for it and using it in your output. For example:

<cffunction name="end" access="public" output="yes"><cfset var ii = 0>
	<div id="sidebar">
		<cfif StructKeyExists(,"aSideBarImages")>
			<cfloop index="ii" from="1" to="#ArrayLen(aSideBarImages)#">
				<div><img src="#aSideBarImages[ii].src#" alt="#aSideBarImages[ii].alt#"></div>