[thelist] CF: Building a <body> tag.

Aaron Johnson ajlist at mindseye.com
Thu Feb 14 12:30:01 CST 2002

Hi Frank,

> I use a system with a template top and template bottom that wraps the
> content in the center. Some of my pages use javascript with onLoad(),
> but not all. Now, the documents load in this order:
> Application
>    -> TemplateTop
>       -> <body>
> My cf scripts go here.
> OnRequestEnd
>   -> TemplateBottom
>      -> </body>
> Can someone suggest a method whereby I can alter the body tag from
> within my scripts? The best solution I can think of is to set the
> body tag to a default variable, and to alter the variable from within
> my scripts, but they run after the TemplateTop has been included. How
> can I go by altering that one variable, after a doc has been
> included? Another kicker is that in every case, there is content
> after the body tag as well.
> General ideas to stimulate creative approaches just as gladly
> accepted as pat solutions.
 -- I'm guessing that you're doing something like this in your
application.cfm and your onrequestend.cfm

(in application.cfm)
<cfinclude template="header.cfm">

(in onrequestend.cfm)
<cfinclude template="footer.cfm">

which is fine... I commonly don't put any includes in the
application.cfm and use it only for directory logic (ie: this directory
has an application.cfm that says that only people in the "managers"
group can access it..)  Instead, I use custom tags in the physical
files... ie:

/articles.cfm would look like

Your content here

This method promotes encapsulation and reuse, (.jeff will argue at the
cost of speed, I'd argue that encapsulation is more important than
millieseconds of speed, but that's in the archives).. but further.. the
solution to your problem:

on pages that need an onload function:
<cf_formatting onload="somefunction()">
your content

on pages that don't need an onload function
your content

Your formatting.cfm custom tag will look like this:

<cfparam name="attributes.onload" default="">

<!--- write the header --->
<cfif thistag.executionmode EQ "start">
<cfoutout><html><head></head><body <cfif
len(attributes.onload)>onload="#attributes.onload#"</cfif> > </cfoutput>

<!--- write the footer --->


If you don't use custom tags and you use includes, encapsulation goes
away.. some people would say that using cfinclude is encapsulation, but
cfinclude doesn't encapsulate variable declarations... so if you do
something like this:

<cfset somevariable = true> in your application.cfm and then include a

<cfinclude template="sometemplate.cfm">

that template can access and modify the variable "somevariable", which
can get really messy if you have multiple developers working on a single
project... anyway..



More information about the thelist mailing list