Passing Data

Just like HTML and CFML tags, custom tags receive parameters via tag attributes. When you call a custom tag, you can pass parameters that automatically become available as variables within the custom tag. ColdFusion has a special variable scope called the attributes scope that refers to reference attributes that have been passed to a custom tag. For example, you can call a custom tag named cf_mytag like this:

<cf_mytag name="Pere Money"
          title="President">

Both name and title are tag attributes. Once the tag is called, these attributes and their associated values are available within the custom tag as attributes.Name and attributes.Title, respectively. Because the custom tag exists within its own scope, you must scope any variables with the attributes prefix in order to access them. Any ColdFusion datatype may be passed to a custom tag as an attribute. This allows you to create custom tags that can manipulate data from a variety of sources, including query objects, arrays, and structures, as well as such simple values as strings and numbers.

Let’s look at a simple example that demonstrates how to pass a few attributes to a custom tag. Example 21-1 calls a custom tag called cf_states (Example 21-2). The tag is used to generate a select box containing U.S. state names along with their associated abbreviations. A number of attributes are passed to the tag: selectname, defaultvalue, defaultlabel, and selected.

Example 21-1.  Passing attributes to the cf_states custom ...

Get Programming ColdFusion MX, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.