Including Out Side Code ColdFusion Help

You’ve probably noticed by now that ColdFusion templates can grow to be much longer and more complex than the average HTML file. In addition, just as with HTML there will inevitably be sections of ColdFusion code that you will want to reuse repeatedly throughout an application or a Web site.

For these reasons, you need to be able to include ColdFusion code from other files in another template. In this chapter, you will look at two ways of doing this: using the CFINCLUOE tag and the CFMOOULE tag. The ways in which these two tags function are different and they are better suited to different purposes. .

In considering the combining of code from different templates, it is also necessary to look at variable scope issues; that is, where does a variable exist in one or both of the templates involved?

Including Code within the Current Templateby Using CFINCLUDE

Anyone who has done advanced work with HTML has probably encountered server parsed HTML. With server-parsed HTML files (generally ending in the extension HTML), it is possible to use a variety of special tags that are processed on the server side before sending the results to the browser .

Most flavors of server-parsed’Hf’Ml, offer one or more tags for including the contents of another HTML file in the current file. This is generally used for commonly recurring elements of pages within a site. For instance, if a site always has the same title or menu bar across the top of each page, this code could be placed in a single file and then included in every page on the site.

The great advantage of this comes in the area of site maintenance: You can modify this global page element for all pages by making changes to a single file. No longer is it necesy to edit all pages or perform global search-and-replace functions to make ilie necessary
alterations to the site.

Cold Fusion offers a similar capability. The Simplest way to achieve this result is by using the CFINCLUDE tag. Although it is possible to devise ways oi us ing Y.JurWeb server’s server-parsed HTML to include a ColdFusion template into an HTML file, it i.s generally best to stick to ColdFusion for the complete solution. This ensures that the correct parsing and processing of both files occurs

The CFINCLUDE Tag

The CFINCLUDEtag is quite simple in that it has only one required parameter: TEMPLATE This parameter indicates the logical path of another ColdFusion template to include in the current template.

The mechanics h re are simple: The code of the specified template is added to the current template at the point where the CFiNCLUDEtag is located and is processed as if it were part of the current page.

Let’s look at how this works. Consider the following code segment. It provides a title banner and menu for a simple site. ColdFusion is used to highlight the correct menu item based on information set in the variable Chosen in the template containing the CFINCLUDE tag.

<H1>Using CFINClUDE</Hl>
<CFIF Chosen is 1>
<STRONG>The Syntax</STRONG>
<CFELSE>
<A HREF-‘syntax.cfm’>The Syntax</A>
</CFIF> I
<CFIF Chosen is 2>
<STRONG>Examples</STRONG>
<CFElSE>
<A HREF-‘examples.cfm’>Examples</A>
</CFIF> I
<CFIF Chosen is 3>
<STRONG>Alternatives</STRONG>
<CFElSE>
<A HREF-‘alternatives.cfm’>Alternatives</A>
</CFIF>
<HR>

Of course, an immediate problem appears to exist with this template: None of the CFIF tags will work because the Chosen variable does not exist. But this is fine because the template is designed to be included in the other templates in the site. For instance Listing 8.1 is the home page of the site that includes the menu and title template (which we will call head. cfm).

<CFSET Chosen-D>
<CFINClUDE TEMPLATE-‘head.cfm’)

This site is designed to provide reference information about the CFINClUDE tag. Choose an option from the menu above.

The end result is that the code from the head. cfm template is included in the home page template and is processed as one large template that looks like Listing 8 2.

<H1>Using CFINCLUDE</H1>
<CFIF Chosen is 1>
<STRONG>The Syntax</STRONG>
<CF’ESE>
<A HREF-‘syntax.cfm’>The Syntax</A>
</CFIF> I
<CFIF Chosen is 2>
<STRONG>Examples</STRONG>
<CFELSE>
<A HREF-‘examples.cfm’>Examples</A>
</CFIF> I
<CFIF Chosen is 3>
<STRONG>Alternatives</STRONG>
<CFELSE>
<A HREF-‘alternatives.cfm’>Alternatives</A>
</CFIF>
<HR>

This site is designed to provide reference information about the CFINCLUDE tag. Choose an option from the menu above.

FIGURE 8.1

FIGURE 8.1

It should be noted that the code that generates the simple menu in your head. cfm template (the series of CFIF tags) is designed to highlight the current selection with a strong font if Chosen is set to 1, 2 or 3 (indicating which selection has been chosen) or to highlight no selection if the value of the variable is set to anything else.

In the home page example you just looked at, Chosen is set to 0 and the result is that all three of the menu choices are active links. By way of comparison, consider Listing 8.3 in which Chosen is set to 3.

<CFSET Chosen-3>
<CFINCLUDE TEMPLATE-‘head.cfm’)
<HR>

This site is designed to provide reference information about the (FINCLUDE tag. Choose an option from the menu above.

This produces results like those in Figure 8.2. Notice how the third option in the menu is highlighted with a bold font and is not a link

FIGURE 8.2

FIGURE 8.2

Posted on November 14, 2015 in Including Out Side Code

Share the Story

Back to Top
Share This