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 19-1 calls a
custom tag called CF_DisplayEmployeeInfo
(Example 19-2). A number of attributes are passed to the
tag: Name
, Title
,
Department
, Email
, and
PhoneExt
.
Example 19-1. Passing Attributes to the CF_DisplayEmployeeInfo Custom Tag
<CF_DisplayEmployeeInfo Name="Pere Money" Title="President" Department="Executive Management" ...
Get Programming ColdFusion 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.