Posted in ColdFusion | Posted on 09-03-2007 | 8,700 views
Last week or so a reader asked if I would quickly demonstrate how I use custom tags for layout. This is something I've done for many years now and is typically how I how handle ensuring a web site can easily maintain a consistent look. The idea is simple - use the fact that custom tags can "wrap" other content. How is this done? Consider this code block:
The custom tag, bold, is used at the beginning and the end of the text block. The last tag is used with a slash just like you see in normal HTML tags that wrap content.
ColdFusion provides a ThisTag scope that provides information about the custom tag. In this case, it tells us if the tag is in a start or end mode. This is done via the executionMode value. So let's look at a layout custom tag. I call this my Pretty in Pink theme:
2
3<html>
4
5<head>
6<title>Test</title>
7</head>
8
9<body bgcolor="#d95ff3">
10
11<cfelse>
12
13</body>
14</html>
15
16</cfif>
The file is split in half with the CFIF statement. The first half runs when thisTag.executionMode is "start", and the second half will run when it is "end" (or any value really, but that's the only other value). So to use my layout tag, I can simply do this:
2This is a test.
3</cf_layout>
Now typically my layout tags perform a bit more work. For example - they typically display a title as well. Consider this modified version:
2
3<cfif thisTag.executionMode is "start">
4
5<html>
6
7<head>
8<cfoutput><title>#attributes.title#</title></cfoutput>
9</head>
10
11<body bgcolor="#d95ff3">
12
13<cfoutput><h1 style="color: white;">#attributes.title#</h1></cfoutput>
14
15<cfelse>
16
17</body>
18</html>
19
20</cfif>
All I've done is defined a variable, attributes.title, and then I use that in both the head area and within an H1 tag. To use this attribute, I just change my files to do this:
2This is a test.
3</cf_layout>
So that's it. Just a quick note - this can be useful in cases even when you use CSS for everything (markup and layout). Even in 100% pure CSS sites, you still need a head block, you still need to point to the CSS file, etc. So I'd still use the layout tag to get those items into the file.


That being said - using CT as opposed to header or footer file feels better to me. I like how I have the template in one file. This makes it easier to debug layout issues too as I don't have to switch between two files.
I'm sure it all comes down to personal preference tho...
<ui:Container Title="PodTitle">
This is content in the pod
</ui:Container>
Sometimes, I display search results in the container and I need to have some sort of pagination. So I built a sub-tag to my container tag:
<ui:Container Title="PodTitle">
<ui:Pagination StartRow="21" MaxRows="20">
This is content in the pod
</ui:Container>
In this code snippet, the 'container' tag reads the 'pagination' tag, and creates a perfectly and consistently formatted pagination element within it.
<do any page action/>
<set page specific vars/>
<header include/>
<content/>
<footer include/>
<cf_header>
</cf_header>
:executionmode start:
<html>
<head>
:executionmode end:
put extra head information here
</head>
<body>
I do not trust the cfheader tag for this because I had major issues with this tag using proxy servers and firewalls years ago :-) maybe it's better these days but I stick with this method because it works like a charm.
I can see how this sort of speak might make some programmers cringe ... but the productivity, elegance, and learning curves are hard to ignore.
[Add Comment] [Subscribe to Comments]