Reusing Code with the CFINCLUDE Tag

Team-Fly    

ColdFusion® MX: From Static to Dynamic in 10 Steps
By Barry Moore
Table of Contents
Step 1.  The Basics


Reusing Code with the <CFINCLUDE> Tag

Another great feature of ColdFusion is the capability to reuse code quickly and easily. As your web site begins to grow in size and complexity, you should strive to reuse code that appearsor that you think might appearmore than once throughout your web site. Common examples of code reuse include web page headers and footers, logos, copyright information, or anything that appears repeatedly.

Reusing code provides you with these benefits:

  • Ease of maintenance. Just make a change to one file, and it changes everywhere it is used throughout the site.

  • Speed. You do not have to constantly copy and paste information across several pages.

  • Fewer mistakes. Type and debug code just once.

There are several ways in which we can reuse code with ColdFusion, but for the time being, let's just look at one: the <CFINCLUDE> tag. The <CFINCLUDE> tag is used to include code from one template inside another. You can use this tag to include HTML, CFML, JavaScript, text, images, queries, or just about anything you would normally find in a ColdFusion template. <CFINCLUDE> does not require a closing tag and has only one attribute: TEMPLATE.

The syntax of <CFINCLUDE> is as follows:

 <CFINCLUDE TEMPLATE = "Copyright.cfm">

The TEMPLATE attribute indicates the name of the file or template to be included. Note that only relative paths (such as ../footer.cfm) are currently supported (see Listing 1.3).

There are two files involved in a <CFINCLUDE> statement: the template that uses the <CFINCLUDE> tag, known as the calling template, and the file or template that is being called, known as the included page. When using a <CFINCLUDE> tag, the code from the included page is slotted into the calling template at the location of the <CFINCLUDE> tag, just as if you had used copy and paste commands and put the code there yourself.

NOTE

Because using a <CFINCLUDE> tag is essentially the same as executing copy and paste commands, you must be careful that codesuch as structure tags from the calling and included pagesdoes not conflict. For example, if the calling template includes a set of opening and closing <HTML> and <BODY> tags, make sure that the included page does not have these as well.


Listing 1.3 <CFINCLUDE> Sample Code for Calling.cfm
 <HTML>  <HEAD>      <TITLE>Calling Page</TITLE>  </HEAD>     <BODY>     <H1>This is my body content</H1>     <HR>     <!--- include copyright statement --->     <CFINCLUDE TEMPLATE="Footer.htm">     </BODY>  </HTML>  <FONT SIZE="-1">Copyright notice &copy; 2002</FONT>

When viewing the page in a browser, you see the included code combined with the code from the calling template (see Figure 1.12).

Figure 1.12. Calling.cfm browser display.

graphics/01fig12.gif

NOTE

When using <IMG> tags within included code, be aware that any relative paths for the SRC attribute must be correct in relation to the calling page; otherwise, the image will not be displayed. You can use absolute paths for the SRC attribute to avoid this problem.



    Team-Fly    
    Top
     



    ColdFusion MX. From Static to Dynamic in 10 Steps
    ColdFusion MX: From Static to Dynamic in 10 Steps
    ISBN: 0735712964
    EAN: 2147483647
    Year: 2002
    Pages: 140
    Authors: Barry Moore

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net