Creating the Tax Computation Tag

 < Day Day Up > 



Now it's time to create the actual tag, a process that's similar to creating a standard ColdFusion template. For the sake of simplicity, assume that your custom tag needs to compute tax for only three states, California, Oregon, and Arizona. Any other state will be taxed at a rate of 0. Also assume that all cities and counties in a given state share one tax rate.

As you might guess, the tag needs to use <cfif> to determine whether the state supplied by the caller page is one of those that requires tax. Start by creating a new ColdFusion document in Dreamweaver. Save it in the folder c:\CfusionMX\CustomTags\ and enter the code you see in Listing 53-2.

Listing 53-2: tax_compute.cfm

start example
<cfif attributes.state is 'CA'>      <cfset tax_rate = .0725> <cfelseif attributes.state is 'AZ'>      <cfset tax_rate = .083> <cfelseif attributes.state is 'OR'>      <cfset tax_rate = 0> <cfelse>      <cfset tax_rate = 0> </cfif>     <cfset caller.tax = attributes.product_total * tax_rate>
end example

Caution 

Don't forget that the filenames you use for your custom tags should not include the cf_ prefix. The prefix is used only when calling the tag from a template.

As Listing 53-2 illustrates, the syntax used in a custom tag is standard CFML, with a couple of exceptions:

  • When you want to refer to a value that was passed as a tag's attribute (see the line that calls this tag in Listing 51-1), you refer to it using the special scope attributes before the attribute name.

  • When you want a variable to be available to the page that called the custom tag, you create it using the special scope caller before the variable name.

Recall from the discussion of the caller page (Listing 53-1) that the tag will be sent two attribute values, state and product_total. It uses these with the attributes scope to determine a tax rate for the user's state, and multiplies that value by the product_total. Finally, a new variable, tax, is defined, using the caller scope so that the variable will be available to the calling page.

Tip 

Listing 53-2 uses <cfif> and <cfelse> to check the contents of the state variable. An alternate method that uses less code is to use the <cfswitch> and <cfcase> tags, which are ideal for these type of checks. See your ColdFusion MX documentation for details.

Now that you've created the tag and stored it in ColdFusion's custom tags folder, you can use it with any page in any one of your sites by referencing it like any other ColdFusion tag:

<cf_tax_compute state="some state" product_total="some product total">

Tip 

If you plan to use a tag like cf_tax_compute in an actual production environment, with more complex tax schemes, you could use a simple database table to store the tax rate for each locale, and then use a <cfquery> to look up the relevant rate. That way when tax rates change, you need to update only the database rather than rewrite your custom tag.



 < Day Day Up > 



Macromedia Studio MX Bible
Macromedia Studio MX Bible
ISBN: 0764525239
EAN: 2147483647
Year: 2003
Pages: 491

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net