We've covered most of the fundamental elements of ColdFusion development in this chapter. I've found over the past several
As we move into the
Chapter 5. Reusing Code
In this chapter, we discuss the different ways in which ColdFusion enables you to create sections of code that are portable and easily reused. We also look at some examples of
tag will likely be your first encounter with code reuse when developing ColdFusion applications. The
tag enables you to embed existing ColdFusion templates into your current template by referencing the template
<!---This is the code for the template PageOne.cfm---> <HTML> <HEAD> <TITLE>Page One</TITLE> </HEAD> <BODY> <H1>This is Page One!</H1> </BODY> </HTML> <!---End of PageOne.cfm---> <!---This is the code for the template PageTwo.cfm---> <HTML> <HEAD> <TITLE>PAGE TWO</TITLE> </HEAD> <BODY> <H1>This is Page Two!</H1> </BODY> </HTML> <!---End of PageTwo.cfm--->
As you can see from the
Figure 5.1. Output of PageOne.cfm .
Similarly, running the second of these two templates results in output similar to Figure 5.2.
Figure 5.2. Output of PageTwo.cfm .
Suppose now that the code in PageTwo.cfm had to be included within the PageOne.cfm template exactly as it is currently written. To do this, you could simply write the code like this:
<!---This is the code for the template PageOne.cfm---> <HTML> <HEAD> <TITLE>PAGE ONE</TITLE> </HEAD> <BODY> <H1>This is Page One!</H1> <CFINCLUDE TEMPLATE="PageTwo.cfm"> </BODY> </HTML> <!---End of PageOne.cfm--->
After modifying the code for PageOne.cfm in this way, you're presented with the output shown in Figure 5.3.
Figure 5.3. Output of modified PageOne.cfm .
As you can see, CFINCLUDE enables you to embed the entire content of an existing CFML template at any point in another CFML template.
You can imagine the uses for this type of functionality. Assume that you have created a header table that needs to go at the top of the page on every page within your web application. By simply creating an include to hold this table, each page that needs to display this table can "include" it. This enables you to keep the central code in one place. Thus, if the header table ever needs modification, a modification to the source file also updates all the templates that are "including" this information.
Each CFINCLUDE example to this point has "included" templates that reside in the same directory as the template into which they are included. We'll refer to these as the calling template and the included template. Because the included templates have been in the same directory, we've been able to reference them just by naming them. We have not had to worry at all about traversing the directory structure of our application or web site.
Note, however, that you might not always want to lump all the templates into one directory. Toward that end, developers often use ColdFusion mappings to create an alias for a directory where all their included templates can be stored and easily accessed from
<!--- This is the code for the template PageOne.cfm ---> <HTML> <HEAD> <TITLE>PAGE ONE</TITLE> </HEAD> <BODY> <H1>This is Page One!</H1> <CFINCLUDE TEMPLATE="/AllIncludes/PageTwo.cfm"> </BODY> </HTML> <!--- End of PageOne.cfm --->
The difference might not be readily apparent. By beginning your included template reference with a forward slash (/), ColdFusion
In this book, you'll see many examples in which included templates are referred to by using a ColdFusion mapping. Keep your eyes
ColdFusion mappings are created in the ColdFusion Administrator and apply only to pages that are
Your new mapping appears in the Active ColdFusion Mappings area at the bottom of the screen. You can now reference any template located within that mapped directory in your CFINCLUDE calls.