ColdFusion in Context: Well-Formed Includes

You may have noticed by now that ColdFusion templates have to be well-formed. You can't just cut up a page into arbitrary templates and have it continue to work. What does that mean to you as a developer?

Data

To explore this concept, you'll need some data. Create the table Item with these column names and some dummy data. (Any data will do.)

ITEM
ItemName,ItemNo
apple,a09
pear,p08
pineapple,p10
quince,q10
egg,e06
jam,j11
jello,j19
lime,l01
omelette,o09
pancake,p45
ham,h3
banana,ba2
basil,b88
water,w18

Main

Put this code in main.cfm...

EXPERIMENT
<p>
<cfquery name="getItems" datasource="context">
select ItemName, ItemNo
from Item
order by ItemName
</cfquery>

1) cfoutput query in parent; plain pounded variable in child.<br>
<cfoutput query="getItems">
#ItemName#
<cfinclude template="more.cfm">
</cfoutput>
<p>
2) cfoutput query in parent; plain output of query variable in child.<br>
<cfoutput query="getItems">
#ItemName#
<cfinclude template="more2.cfm">
</cfoutput>
<p>
3) cfloop query in parent; plain output of query variables in child.<br>
<cfloop query="getItems">
<cfinclude template="more3.cfm">
</cfloop>
<p>
4) query in parent; cfoutput query in child.<br>
<cfinclude template="more4.cfm">
<p>
5) bold in one child; end bold in another
<cfinclude template="more5a.cfm">
ABCDEFG
<cfinclude template="more5b.cfm">
<p>
6) begin cfoutput and pound 
variable in one child; end cfoutput in another; watch it die
<cfinclude template="more6a.cfm">
<cfinclude template="more6b.cfm">

Templates

Create the following tiny child templates:

more.cfm...

#ItemNo#

more2.cfm...

<cfoutput>#ItemNo#</cfoutput>

more3.cfm...

<cfoutput>#ItemName# #ItemNo#</cfoutput>

more4.cfm...

<cfoutput query="getItems">#ItemName# #ItemNo#</cfoutput>
more5a.cfm...

<b>

more5b.cfm...

</b>
more6a.cfm...

<cfoutput>#Item#
<!--- this will fail --->

more6b.cfm...

</cfoutput>

Implications for Development

Browse main.cfm and learn from the output...

1) You can't simply include extra output variables by selecting a template of pound-delimited variables; you have to provide something to interpret those variables for output. cfoutput of a query in the parent will not by itself interpret a pounded variable in the child; the raw variable name shows up with pound signs around it.

2) You can't physically nest cfoutput tags on a single page, but you can logically nest them by calling a template that uses them within a pair of cfoutput tags. cfoutput of a query in the parent can control a plain cfoutput tag pair in the child; the interpreted variable name shows up corresponding to the correct row of the query.

3) A loop on a parent page does control execution of a child template. cfloop of a query in the parent can control a plain cfoutput tag pair in the child; the interpreted variable name shows up corresponding to the correct row of the query.

4) You can include an entire output loop in the child based on a query in the parent. cfoutput of a query in the child works just fine based on a query in the parent. (This one looks a bit funny; because, the extra white space introduced by the include in the previous examples is missing.)

5) ColdFusion doesn't care whether HTML tags are paired up; they're interpreted by the client, not by ColdFusion.

6) ColdFusion does want ColdFusion tags to be paired up and will throw an error if they're not.

A ColdFusion tempate has to be well-formed. Pair up your ColdFusion tags on any one page. A cfoutput tag pair in a parent page does control execution of child templates and makes variables available to them. However, it does not interpret (expand) pounded variables in the children; the child must have its own. =Marty=