|By Steve Bryant||
|November 29, 2005 05:45 PM EST||
Most sites have similar issues related to the site layout. I have seen many solutions that solve some challenges related to layout, but fail to handle others elegantly. I have found that using layout components elegantly solves all of the layout problems I have faced.
What Do I Want?
Here are my goals for handling layouts. I want to:
- Make edits to the layout in one place
- Easily switch layouts (per page, section, etc.)
- Switch to non-HTML formats (PDF, Word, etc.)
- Use any ColdFusion tags (like cfflush)
- Do without mappings and relative paths
- Add HTML to the <head>
- See all of the layout in one file
- Avoid passing all page calls through one file (like index.cfm)
- HTML: www.bryantwebconsulting.com/cfcs/
- PDF: www.bryantwebconsulting.com/cfcs/?format=PDF
- Word: www.bryantwebconsulting.com/cfcs/?format=Word
- Excel: www.bryantwebconsulting.com/cfcs/?format=Excel
As you will see, layout components also make it easy to use a different layout on your home page than on other pages. Similarly, the login page for your admin section can use your default site layout (instead of the admin layout).
Before I discuss layout components, let me explain why I don't use more "obvious" techniques.
Specifically, I recommend you avoid the temptation to put output code in Application.cfm (and OnRequestEnd.cfm). When a page needs an alternate layout (a pop-up, home page, etc) then you have to add an exception to Application.cfm, which can lead to unwieldy code.
For instance, some programmers use a different layout based on the filename being requested (e.g., "index.cfm" gets a different layout from the rest). This is problematic when you or your client wants to make a quick copy ("index2.cfm") to test a change.
While you could determine layout using the directory structure, this is also limiting. A home page may need a different layout than the rest of the site or the client might decide to change the layout of any page. You should be able to change a layout without moving the page (lest you jeopardize links and bookmarks).
I therefore recommend that you call output code from your page (whether you use layout components or not) to ensure this flexibility. Using layout components doesn't prevent you from including your output from Application.cfm, but I highly recommend that you include your output from your page.
Let's Take a Look
I frequently need to add code to the <head> of just one page. Using <cfhtmlhead> works, but seems clunky. I would prefer code as shown in Listing 1. My home page, using a different layout, might look like Listing 2.
In any event, I must first initialize the layout component:
<cfset layout = CreateObject("component","layouts.default").init()>
This instantiates a layout using layouts/default.cfc (from the root of your site). This can be called from Application.cfm (creating the layout variable doesn't cause any output).
The code for default.cfc is in Listing 3. The code for Home.cfc (the layout used on our home page) would be the same with slightly different HTML.
As you can see, a layout component is just a basic HTML page with some component tags in the mix. Within a week of working layout components, I learned to ignore the cffunction and cfcomponent tags and just see the HTML. You can see the resulting HTML for Listing 1 in Listing 4.
How Does It Work?
As I am sure you can tell by looking at the component code and the resulting HTML, each method of the layout component is called in turn and outputs HTML into the page.
Normally, a CFC shouldn't output anything directly. In this case, however, the whole point of a layout component is output. So, the output of each cffunction is "yes" (where it would normally be "no").
It is important that </head> is at the top of the body() method. This allows me to put HTML into the head of the document by placing the code between layout.head() and layout.body().
Of course, we still need to cover how to switch layouts...
You might have noticed that default.cfc has an extends attribute in the cfcomponent tag (look at the first line in Listing 3). That means that it inherits some behavior from the layout.cfc in the same directory (see Figure 1 for an example inheritance tree).
The methods that are common to all of our layout components go here. Among those is the switchLayout() method that we used at the top of our home page to switch to the home page layout (Home.cfc).
If you wanted to switch to a Word layout (to output to Word instead of HTML), you would switch to the Word layout like so:
<cfset layout = layout.switchLayout("Word")>
This would use Word.cfc for your layout, which would be a layout component with very basic HTML and the following tag at the start of the output:
<cfcontent type="application/msword" reset="Yes">
Make sure to call layout.switchLayout() before layout.head(). If you want to take a look at the code for layout.cfc, you can see it in Listing 5.
How Does This Compare?
The usage of layout components that I have described is most similar to using cfinclude to include a header and footer, with some notable advantages. You don't have to worry about using different paths depending on the location of your file. You can see the whole layout in one file, allowing you to easily see where your divs and table cells open and close. You can switch your layout using code in just one place.
|CFDJ News Desk 11/29/05 06:57:35 PM EST|
Handling Site Layouts in ColdFusion with Layout Components. Most sites have similar issues related to the site layout. I have seen many solutions that solve some challenges related to layout, but fail to handle others elegantly. I have found that using layout components elegantly solves all of the layout problems I have faced.
|Steve Bryant 11/29/05 12:13:51 PM EST|
I haven't used Fusebox 4. From my understanding, though, the restriction on cfflush has been circumvented in Fusebox 4. The need to run al files through index.cfm, however, is central to Fusebox.
- Where Are RIA Technologies Headed in 2008?
- The Next Programming Models, RIAs and Composite Applications
- AJAX World RIA Conference & Expo Kicks Off in New York City
- Constructing an Application with Flash Forms from the Ground Up
- Building a Zip Code Proximity Search with ColdFusion
- Personal Branding Checklist
- CFEclipse: The Developer's IDE, Eclipse For ColdFusion
- Has the Technology Bounceback Begun?
- Adobe Flex 2: Advanced DataGrid
- i-Technology Viewpoint: We Need Not More Frameworks, But Better Programmers
- Web Services Using ColdFusion and Apache CXF
- Cloud People: A Who's Who of Cloud Computing