Welcome!

You will be redirected in 30 seconds or close now.

ColdFusion Authors: Yakov Fain, Jeremy Geelan, Maureen O'Gara, Nancy Y. Nee, Tad Anderson

Related Topics: ColdFusion

ColdFusion: Article

Handling Site Layouts in ColdFusion with Layout Components

A flexible way to handle layouts for your sites

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)
Here is a quick example of switching to non-HTML formats: I didn't have to switch the format using the URL, but it made a handy example. This makes "download to Excel" functionality a breeze.

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).

Include Interlude
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...

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.

More Stories By Steve Bryant

Steve Bryant is the founder and CEO of Bryant Web Consulting LLC (www.bryantwebconsulting.com) and teaches ColdFusion at Breakaway Interactive (www.breakawayinteractive.com). He got his BA in philosophy at Oklahoma State University. Steve, one of the top ColdFusion developers in the country, still has no idea how that led to a career in Web development. Steve blogs at steve.coldfusionjournal.com as one of CFDJ's published bloggers.

Comments (2)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.


@ThingsExpo Stories
SYS-CON Events announced today that Technologic Systems Inc., an embedded systems solutions company, will exhibit at SYS-CON's @ThingsExpo, which will take place on June 6-8, 2017, at the Javits Center in New York City, NY. Technologic Systems is an embedded systems company with headquarters in Fountain Hills, Arizona. They have been in business for 32 years, helping more than 8,000 OEM customers and building over a hundred COTS products that have never been discontinued. Technologic Systems’ pr...
SYS-CON Events announced today that CA Technologies has been named “Platinum Sponsor” of SYS-CON's 20th International Cloud Expo®, which will take place on June 6-8, 2017, at the Javits Center in New York City, NY, and the 21st International Cloud Expo®, which will take place October 31-November 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. CA Technologies helps customers succeed in a future where every business – from apparel to energy – is being rewritten by software. From ...
The taxi industry never saw Uber coming. Startups are a threat to incumbents like never before, and a major enabler for startups is that they are instantly “cloud ready.” If innovation moves at the pace of IT, then your company is in trouble. Why? Because your data center will not keep up with frenetic pace AWS, Microsoft and Google are rolling out new capabilities In his session at 20th Cloud Expo, Don Browning, VP of Cloud Architecture at Turner, will posit that disruption is inevitable for c...
SYS-CON Events announced today that Cloudistics, an on-premises cloud computing company, has been named “Bronze Sponsor” of SYS-CON's 20th International Cloud Expo®, which will take place on June 6-8, 2017, at the Javits Center in New York City, NY. Cloudistics delivers a complete public cloud experience with composable on-premises infrastructures to medium and large enterprises. Its software-defined technology natively converges network, storage, compute, virtualization, and management into a ...
Keeping pace with advancements in software delivery processes and tooling is taxing even for the most proficient organizations. Point tools, platforms, open source and the increasing adoption of private and public cloud services requires strong engineering rigor - all in the face of developer demands to use the tools of choice. As Agile has settled in as a mainstream practice, now DevOps has emerged as the next wave to improve software delivery speed and output. To make DevOps work, organization...
SYS-CON Events announced today that Loom Systems will exhibit at SYS-CON's 20th International Cloud Expo®, which will take place on June 6-8, 2017, at the Javits Center in New York City, NY. Founded in 2015, Loom Systems delivers an advanced AI solution to predict and prevent problems in the digital business. Loom stands alone in the industry as an AI analysis platform requiring no prior math knowledge from operators, leveraging the existing staff to succeed in the digital era. With offices in S...
The explosion of new web/cloud/IoT-based applications and the data they generate are transforming our world right before our eyes. In this rush to adopt these new technologies, organizations are often ignoring fundamental questions concerning who owns the data and failing to ask for permission to conduct invasive surveillance of their customers. Organizations that are not transparent about how their systems gather data telemetry without offering shared data ownership risk product rejection, regu...
SYS-CON Events announced today that Interoute, owner-operator of one of Europe's largest networks and a global cloud services platform, has been named “Bronze Sponsor” of SYS-CON's 20th Cloud Expo, which will take place on June 6-8, 2017 at the Javits Center in New York, New York. Interoute is the owner-operator of one of Europe's largest networks and a global cloud services platform which encompasses 12 data centers, 14 virtual data centers and 31 colocation centers, with connections to 195 add...
SYS-CON Events announced today that SoftLayer, an IBM Company, has been named “Gold Sponsor” of SYS-CON's 18th Cloud Expo, which will take place on June 7-9, 2016, at the Javits Center in New York, New York. SoftLayer, an IBM Company, provides cloud infrastructure as a service from a growing number of data centers and network points of presence around the world. SoftLayer’s customers range from Web startups to global enterprises.
SYS-CON Events announced today that CrowdReviews.com has been named “Media Sponsor” of SYS-CON's 20th International Cloud Expo, which will take place on June 6–8, 2017, at the Javits Center in New York City, NY. CrowdReviews.com is a transparent online platform for determining which products and services are the best based on the opinion of the crowd. The crowd consists of Internet users that have experienced products and services first-hand and have an interest in letting other potential buyers...
SYS-CON Events announced today that T-Mobile will exhibit at SYS-CON's 20th International Cloud Expo®, which will take place on June 6-8, 2017, at the Javits Center in New York City, NY. As America's Un-carrier, T-Mobile US, Inc., is redefining the way consumers and businesses buy wireless services through leading product and service innovation. The Company's advanced nationwide 4G LTE network delivers outstanding wireless experiences to 67.4 million customers who are unwilling to compromise on ...
SYS-CON Events announced today that Infranics will exhibit at SYS-CON's 20th International Cloud Expo®, which will take place on June 6-8, 2017, at the Javits Center in New York City, NY. Since 2000, Infranics has developed SysMaster Suite, which is required for the stable and efficient management of ICT infrastructure. The ICT management solution developed and provided by Infranics continues to add intelligence to the ICT infrastructure through the IMC (Infra Management Cycle) based on mathemat...
SYS-CON Events announced today that SD Times | BZ Media has been named “Media Sponsor” of SYS-CON's 20th International Cloud Expo, which will take place on June 6–8, 2017, at the Javits Center in New York City, NY. BZ Media LLC is a high-tech media company that produces technical conferences and expositions, and publishes a magazine, newsletters and websites in the software development, SharePoint, mobile development and commercial UAV markets.
SYS-CON Events announced today that Telecom Reseller has been named “Media Sponsor” of SYS-CON's 20th International Cloud Expo, which will take place on June 6–8, 2017, at the Javits Center in New York City, NY. Telecom Reseller reports on Unified Communications, UCaaS, BPaaS for enterprise and SMBs. They report extensively on both customer premises based solutions such as IP-PBX as well as cloud based and hosted platforms.
In his keynote at @ThingsExpo, Chris Matthieu, Director of IoT Engineering at Citrix and co-founder and CTO of Octoblu, focused on building an IoT platform and company. He provided a behind-the-scenes look at Octoblu’s platform, business, and pivots along the way (including the Citrix acquisition of Octoblu).
"I think that everyone recognizes that for IoT to really realize its full potential and value that it is about creating ecosystems and marketplaces and that no single vendor is able to support what is required," explained Esmeralda Swartz, VP, Marketing Enterprise and Cloud at Ericsson, in this SYS-CON.tv interview at @ThingsExpo, held June 7-9, 2016, at the Javits Center in New York City, NY.
SYS-CON Events announced today that HTBase will exhibit at SYS-CON's 20th International Cloud Expo®, which will take place on June 6-8, 2017, at the Javits Center in New York City, NY. HTBase (Gartner 2016 Cool Vendor) delivers a Composable IT infrastructure solution architected for agility and increased efficiency. It turns compute, storage, and fabric into fluid pools of resources that are easily composed and re-composed to meet each application’s needs. With HTBase, companies can quickly prov...
Web Real-Time Communication APIs have quickly revolutionized what browsers are capable of. In addition to video and audio streams, we can now bi-directionally send arbitrary data over WebRTC's PeerConnection Data Channels. With the advent of Progressive Web Apps and new hardware APIs such as WebBluetooh and WebUSB, we can finally enable users to stitch together the Internet of Things directly from their browsers while communicating privately and securely in a decentralized way.
DevOps is often described as a combination of technology and culture. Without both, DevOps isn't complete. However, applying the culture to outdated technology is a recipe for disaster; as response times grow and connections between teams are delayed by technology, the culture will die. A Nutanix Enterprise Cloud has many benefits that provide the needed base for a true DevOps paradigm.
What sort of WebRTC based applications can we expect to see over the next year and beyond? One way to predict development trends is to see what sorts of applications startups are building. In his session at @ThingsExpo, Arin Sime, founder of WebRTC.ventures, will discuss the current and likely future trends in WebRTC application development based on real requests for custom applications from real customers, as well as other public sources of information,