Introduction to ColdFusion Markup Language - The cflocation tag
(Page 7 of 7 )
Try entering the following code into a new Document window in Dreamweaver:
When you test the page in a browser window, you will notice it bounce automatically to whatever URL you specify. The<cflocation>tag is very useful in situations where you want to insert a record into a database and then redirect the user to a confirmation page, or else redirect the user to the next step in a wizard. It is also useful after inserting a new record into a database, because it can prevent the user from refreshing the action page and submitting duplicate data. <cfinclude>
The<cfinclude>tag is a very useful tag because it allows you to include the contents of another file in the current page (essentially serving the same function as classic HTML server-side includes). For example, you could build header and footer files and then dynamically include them in each page of your site.
Using<cfinclude>eliminates having to copy and paste the HTML and/or CFML for the header and footer into every page; instead, you can centralize the code and make modifications in only one place. Let's look at an example.
- Create two new ColdFusion files, one namedheader.cfmand the other calledfooter.cfm,in thecfbooksite using Dreamweaver. Make sure that you save these two files in the same directory as theApplication.cfmandindex.cfmfiles that you created in the earlier examples.
- In theheader.cfmfile, enter the following code:
This is our header.<br />
- Then enter the following code in thefooter.cfmfile:
this is our footer.<br />
- Clear the contents of theindex.cfmand type in the following code:
I am the original page.<br />
Create a new line at the beginning of theindex.cfmpage. The code you entered in step 4 should be on line 2 now. Next, select the CFML Basic tab in the Insert bar and click on the cfinclude button, as shown in Figure 3-7.
After clicking on the button, the Tag Editor Cfinclude dialog box will open with a text field named Template and a Browse button to the right, as shown in Figure 3-8.
Click on the Browse button and select theheader.cfmpage that we just created. After completing this step, click OK and OK again to insert this tag into theindex.cfmpage.
Figure 3-7. Using Dreameaver MX 2004 to insert a<cfinclude>tag into a web page
Figure 3-8. The<cfinclude>Tag Editor
- Now use the same procedure to includefooter.cfmat the bottom of the page. Theindex.cfmfile should now look like the following:
I am the original page.<br /> <cfinclude template="footer.com">
When you runindex.cfmin a web browser, you will see the output in Figure 3-9.
Figure 3-9. Our page output, as seen in Internet Explorer 6
You can see that theheader.cfmandfooter.cfmfiles are included in the index.cfm page, processed, and then output into the browser. You can use this technique to embed headers, footers, counters, menus, Flash movies, or pretty much any sort of web content you wish. Be aware that any code in an included file will be executed in the file doing the include as if it were coded right there in the file doing the include, which means that variable-name conflicts could result in undesirable behavior. It is also important to become familiar with relative paths if you aren't already familiar with them." ../" goes up one directory level and "directoryname/" goes down. Just like HTML<img>,<a>, and other tags, relative paths are a good way to specify files in other directories for inclusion. Later in this book, you will learn about CFMappings, which are another way to specify the location of files for inclusion.