Redireding Users to Another Template ColdFusion Help

Indirectly related to controlling the flow of a template is the ability to redirect a user’s browser to another template. This is done using the CFLOCATION tag. The CFLOCATION tag takes two attributes

URL Specifies the URL of the template or HTML to which the user should be redirected.

ADDTOKEN Indicates if a client management token should be added to the specified URL. Discussion of client management takes place in Chapter 17, “Implementing the Cold Fusion Web Application Framework.” Possible values for the attribute are YES and NO

For instance to redirect the user to new template. efm you could use the tag
< OCATIONURL-·newtemplate.efm>

The real question is: When would you need to redirect a user to another document?

There are several application of this technique. Just a few of them are as follows:

• After processing a  from submission, you may want to redirect a user back to a home page or a default page. Forms are discussed in Chapter 15 “Forms.”
• If when processing a form submission, an error in the submitted information is wrong you m. want to redirect the user back to the original form.
• Based on the value of an expression it may be necessary to redirect the user to different parts of the site

Keeping in mind that after the CFLOCATION tag is executed the remainder of the current template will not be executed, you can use multiple CFLOCATIONtags within a template for example, as part of an If-Else structure or a switch-case test

<CFIF TestValue is 1>
<CFLOCATION URL-‘templatel.cfm’>
<CFELSE>
<CFLOCATION URL-‘template2.cfm’>
</CFIF>

Using CFABORT and CFEXT:

Finally you need to consider CFABORTand CFEXIT. which provide ways to prematurely terminate the execution of a template..

The CFABORTtag causes the execution of a template to stop and any output generated up to that point to be displayed. For this to happen, no attributes are needed: Simply use <CFABORT>at the appropriate place in your template

CFABORTth, ough, also can be used in a slightly more complicated way. Using the SHOWERROatRtribute, you can specify an error message to be displayed at the point where CFABORTca used the template to terminate. For instance, consider the following code:

The template is executing

<CFABORT SHONERROR-‘The template has aborted.’>
The rest of the template

This produces results ~e those in Figure 7.1.

FIGURE 7.1

FIGURE 7.1

To go one step further use the CFERROR tag. CFERROR allows the redirection to a specified error template when used with the CFABORT tag and its SHOWERROR attribute. The CFERROR tag is discussed in more detail in Chapter 18, “Implementing Error Control.”

CFEXIT is, in many ways, similar to CFABORT in that it causes the premature termination of a template. Used in the context of a regular template (as opposed to a custom tag), CFEXIT has the same effect as <CFABORT>. CFEXIT though, is designed to be used within a custom tag. Custom tags are discussed in more detail in Chapter 31, “Building ColdFusion Custom Tags

Where Do We Go from Here?

In this chapter you crossed an important milestone: You learned how to create templates that do more than sequentially proceed from start to finish every time. You learned how to alter the flow of a template on the basis of conditions

This is an important ability and you will continue to build on it in Chapter 13, “Looping,” which discusses another mechanism for altering the flow of a template .

In the next chapter you will look at how it is possible to include code from other templates in a current template. This ability makes it possible to reuse segments of code in “multiple templates while writing the segment only once and maintaining it in a single location. In large sites this ability is invaluable because it eases site development and management .

Posted on November 14, 2015 in Controlling The Flow Of Your Templates

Share the Story

Back to Top
Share This