Understanding Variable Scope Issues ColdFusion Help

In this chapter especially in the discussion of the CFMODULE tag you have seen examples of the implications of variable scope even if we haven’t explicitly referred to them.

The concept of scope is fairly simple Scope simply refers to the context in which a variable exists or doesn’t. In its most simple state, a variable’s scope starts when it is first declared (for instance, when a vahre is assigned to it using CFSET) in a template and lasts
to the end of the template.

This is best understood by example. In the following example, the CFOUTPUT tag can be used to display the value of the variable SAMPLE only because the variable SAMPLE came into existence earlier in the template using the CFSET tag:

<CFSET SAMPLE=’Some Text’)

However if the order of these lines IS reversed then CFOUTPUT is trying to access the variable SAMPLE outside the scope of the variable (that is before it has been created):

<CFSET SAMPLE=’Some Text’>

In this case an error similar to the one in Figure 8.3 will be produced.



More subtleties to this exist when more.than one template are involved. When you used CFINCLUDE the code of the included template became part of the calling template. This means that it is part of the same scope. The code of the calling template can directly refer to variables contained in the code of the template being included and the other way around.

This is why for instance when CFINCLUDEwas used the template was able to directly reference the variable Chosen which was used to generate the header and menu.

Using CFMODUL is more complicated however. Because the code of the custom tag is executed independently of the code in the calling template, they have different scopes. Therefore, there is no way for the code in the custom tag to directly access values in the calling template (this can be done only indirectly through ca llers), and values need to be passed to the custom tag through the use of attributes.

This is an important distinction because it allows for variables to exist with the same name in both the calling template and custom tag independently of each other. They can contain different values and do not interfere with each other.

Recall our custom tag for.outputting a menu and header:

<CFSET Chosen – attributes. Section>

<H1>Using CFINCLUDE</H1>
<CFIF Chosen ;s 1>
<A HREF-‘syntax.cfm’>The Syntax</A>
<CFIF Chosen ;s 2>
<A HREF-‘examples.cfm’>Examples</A>
<CFIF Chosen ;s 3>
<A HREF-‘alternatives.cfm’)Alternatives</A>

Here you have created a variable called Chosen within the context of the custom tag. Consider then the following home-page example:

<CFSET Chosen2’Test’)
<CFMODULE NAME~’menu’ Section-‘O’) .

CFMODULE tag provides an alternative way of including outside code in the current template

In the home page the variable Chosen is created and assigned the value Test. Next the custom tag is called and passed the Section attribute. Within the custom tag the value of the Section attribute is assigned to a variable called Chosen. This variable has a different scope than the. one set in the calling template however and when the custsm tag finishes executing the’ variable Chosen.in the calling template retains the value Test.

Taken a step further the custom tag could even access the variable Chosen from the calling template independently of the variabte Chosen created within the custom tag. That is, in the custom tag Chosen and caller . Chosen are separate and distinct variables with different values.

Where Do We Go from Here

In this chapter you have learned about two useful tags for combining more than one template: CFINCLUDEfor including one template in another and CFMODULfoEr using another template as a Custom tag.

In the next chapter, you will move on to an extremely important aspect of ColdFusion: creating databases and tables.

Databases are what give ColdFusion its power. With databases, ColdFusion applications can manipulate and process large amounts of information quickly and easily for the user and at the request of users. This allows the creation of sophisticated interactive dynamic Web sites.

Posted on November 14, 2015 in Including Out Side Code

Share the Story

Back to Top
Share This