Dealing with Nonexistent Parameters
The final thing to consider when passing data between templates is how to check for and handle missing parameters. When dealing with data passed via URL or form field, it is entirely possible that an application template won’t receive a parameter it is expecting. You should build mechanisms into your application templates to account for this possibility. As always, there are a few ways you can go about this.
The first method involves using the
cfparam tag to assign a default value to any
parameters expected by your application template. That way, if an
expected parameter isn’t passed, a default value is
automatically assigned, and the application can continue processing.
If you remember, the
cfparam tag was covered
earlier in Chapter 2. Additional information on
cfparam tag can be found in Appendix A.
cfparam tag is
great for assigning a default value if one doesn’t
exist; however, it doesn’t allow you to display an
error message or perform an alternate action if an expected variable
doesn’t exist. The next method uses the
IsDefined( ) function to check for the existence
of a variable before allowing processing of the template to continue.
If the expected variable exists, the template is processed. If not,
an error message is written to the browser. For example, if you have
a template that is expecting a URL parameter named
ArticleID to be passed in, you can use the following code to output an error message if the parameter isn’t present: ...