The change dline <CF _ Multiply New NUMBERS = ” # Form . Number1# #Form.Number2# #Form. Number3#”>, represents the change you are going to make to ~ur custom tag. As stated previously you are going to send a list of numbers to your custom tag. In this case your list is comprised of the three form variables from your application. You will also notice that you are going to use a new custom tag, aptly named Multiply New.This is simply done so that you can compare the versions. Listing 31.5 shows the new version of your custom tag.
ThiS ability to return results to different variables in the calling template is not something you have to do in a custom tag; again, it’s just a nice feature to have.
The next line, <CFSET Fi na’l Resu’l t – ‘1>, will be used by your tag to keep the current result. As you multiply each-set of numbers, you need a-way to store the total number. Because every number multiplied by 1is equal to itself, setting Final Result to 1 has no effect on your result.
Note that there are two ways of leaving a custom tag:’ You can use CFEXITor CFABORT_. If you use CFooT, the custom tag will stop executing but will not stop the calling tem- ‘ _ plate. In this case you use CFABORTbecauseyou assume that if the calling template has . , made this mistake, everything else should stop. In other cases, you may need to alter- , nate between using CFEXITand Cj:~BORTdepending on the nature of particular custom tags and the nature of the errors you are encountering.
But each of those methods is not exactly what you want. In both cases you are assuming that your 1iS~will have at least’ t.’v0 elements. But what if you want your tag to be handle lists with a single element as well? Then neither approach is the perfect solution.
passed in the Numbers attribute is a simple string (as opposed to a more complex data ” type such as an array). You do this with the Is,Si mple Value function. If there is an error the CFABORTtag provides the means to halt execution and display an error message.
The Is Numeric function is a quick and simple way to do that. Again, if you encounter a problem with the data, you throw an error message to the user and CFABORTout of the tag. Also note that when you send the error message, you specifically tell the user which item is invalid.
As you can see to create a dynamic variable in ColdFusion you can simply wrap the variable name in quotes. In this case if the user does not set a Return attribute and it defaults to RESULT,y~Llcan imagine the previous line as reading <CFSET Call e r ‘. . RESULT- Fi na1Resu1 t>. Remember that this is not necessary, but it’s a nice feature to add to your tag.
The modifications to your calling template are minor. You simply added extra form fields and CFPARAMfosr the form fields. You then passed the new form fields to the Multiply New custom tag. This tag ~asily handled the new numbers and returned the results to the template.