Why Use Custom Tags?


The three main reasons for using Custom Tags are

  • Reusability

  • Maintainability

  • Hiding complexity

These three reasons are related, the latter two being by-products of the first.

Two of the most important tags that you learn as a beginning ColdFusion developer are <cfquery> and <cfoutput>.

<cfquery> is introduced in Chapter 7, "Using Databases," and <cfoutput> is introduced in Chapter 2, "Working with Variables and Expressions."


The following example shows how those two tags might be used together to query the database for information and then display the results in an HTML table.

 <!--- first, query the database ---> <cfquery name="GetEmployees" datasource="dsn"> SELECT FirstName, LastName, Phone, Email FROM Employees </cfquery> <!--- second, display the table caption ---> <h1>All Company Employees</h1> <!--- then display the query results in a table ---> <table border="1">  <tr>   <th>First Name</th>   <th>Last Name</th>   <th>Phone Number</th>   <th>Email Address</th>  </tr>  <cfoutput query="GetEmployees">   <tr>    <td>#FirstName#</td>    <td>#LastName#</td>    <td>#Phone#</td>    <td>#Email#</td>   </tr>  </cfoutput> </table> 

You have probably used similar code a thousand times in your ColdFusion applications, which makes it a good candidate for a Custom Tag. Instead of typing all that code every time, you could call this functionality by just typing one tag:

 <cf_queryprinttable datasource="HR"  TABLENAME="Employees"  columnnames="FirstName, LastName, Phone, Email"  columnheaders="First Name, Last Name, Phone Number, Email Address"  caption="All Company Employees"> 

This Custom Tag will use the attributes passed to it to generate the same results the preceding example did, but this tag is more flexible because you can reuse the code by merely passing it different values through the attributes each time you need the functionality.

Every programmer has looked at code that he or she built a year previously and cringes. As you develop your skills, you find better and more efficient ways of coding, and you'll want to return to your old code and improve it. If you do not use Custom Tags, you will have to hunt through all your code to find and update the multiple places where you embedded code for one piece of functionality. That could take weeks or even months, depending on the complexity of your application. However, if you used Custom Tags, you can open one template with the functionality code, and update itand then all the applications that use it will immediately benefit from the change.

NOTE

Custom Tag execution may be slightly slower than that of ColdFusion Components or User Defined Functions. As such, they should not be used for all reuse, but for the right situations they are invaluable.


If you work as part of a team of developers, Custom Tags can have an additional benefit. Teams often consist of people with different skill sets and a varying degree of proficiency even within the same skill set. In a situation like this, it is often beneficial to have senior developers create Custom Tags, which junior developers can then use to perform common functions.

A senior developer can even encode the Custom Tag template so that the complexity of the code does not confuse junior developers.

Template encoding is discussed in Chapter 1, "Web Technology and Terminology."




Macromedia ColdFusion MX 7 Certified Developer Study Guide
Macromedia ColdFusion MX 7 Certified Developer Study Guide
ISBN: 0321330110
EAN: 2147483647
Year: 2004
Pages: 389
Authors: Ben Forta

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