Simple Custom Tags


In our earlier example, the Custom Tag we discussed used attributes. We'll go into more detail about attributes in the next section. However, the simplest form of Custom Tag takes no attributes at all. Such tags simply perform one task.

Despite their one-track nature, simple Custom Tags can be quite powerful. Consider the Custom Tag <CF_EmbedFields>. This tag takes no attributes, but is indispensable when working with multiple form pages.

TIP

<CF_EmbedFields>, along with the other Custom Tags mentioned in this chapter, can be found on the Macromedia Developer's Exchange at http://devex.macromedia.com/developer/gallery/index.cfm.


URL and FORM variables allow developers to pass information from one page to another, but if information must persist across multiple pages, most developers use APPLICATION, SERVER, SESSION, COOKIE, or CLIENT variables.

FORM and ACTION pages are discussed in Chapter 9, "FORM Variables." SESSION, COOKIE, and CLIENT variables are introduced in Chapter 11, "Session State Management."


The complexity of persistent variables, however, is sometimes overkill for simple problems. For instance, if you have a wizard that takes users through multiple forms, you don't necessarily want to maintain that information in SESSION variables.

The Custom Tag <CF_EmbedFields> forces FORM variables to be passed from one form page to another by embedding the results of one submission as hidden form fields in the next form.

The following code makes up the functionality of <CF_EmbedFields>:

 <!--- Check that FieldNames exists ---> <cfif IsDefined("FORM.FieldNames")>  <!--- Create empty list of processed variables --->  <cfset fieldnames_processed="">  <!--- Loop through fieldnames --->  <cfloop index="form_element" list="#FORM.FieldNames#">   <!--- Try to find current element in list --->   <cfif ListFind(fieldnames_processed, form_element) IS 0>    <!--- Make fully qualified copy of it    (to prevent accessing      the wrong field type) --->    <cfset form_element_qualified="form." & form_element>    <!--- Output it as a hidden field --->    <cfoutput>     <input type="hidden"            name="#form_element#"            value="#Evaluate(FORM_element_qualified)#">    </cfoutput>    <!--- And add it to the processed list --->    <cfset fieldnames_processed=           ListAppend(fieldnames_processed, form_element)>   </cfif>  </cfloop> </cfif> 

This code evaluates the special FORM variable FORM.FieldNames, which is a list of all the names of the FORM variables being passed from a FORM.

FORM variables, specifically the special variable FORM.FieldNames, are discussed in Chapter 9, "FORM Variables." Lists are discussed in Chapter 13, "Lists."


As the code loops over the FORM.FieldNames list, it generates one HTML hidden form field for each field that was passed from the previous form. Using this Custom Tag, you can easily continue to pass information from page to page, essentially maintaining state, without worrying about the complexities of using a database or session state variables.

<CF_EmbedFields> is a simple Custom Tag in that it takes no arguments and returns no data.



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