Returning Data
The caller scope passes data from inside a custom tag back to the calling template. Because custom tags exist within their own scope, you must use the caller scope if you want to make variables you create within your custom tag available to the calling template.
Setting a caller variable is just like setting any other type of ColdFusion variable. You may assign any datatype to a variable in the caller scope. This means that you can pass both simple and complex datatypes from inside a custom tag back to the template that called it.
Let’s create a quick example that demonstrates passing an
attribute to a custom tag, acting on that attribute, and passing a
variable back using the Caller
scope. In Example 19-3, you will find a custom tag called
CF_CountWords
(save the template as
CountWords.cfm
on your server). The tag is
designed to accept a single attribute called
String
, count the number of words in the string,
and return a caller variable back to the calling template with the
number of words in the string. The number of words in the string is
calculated by treating the string as a list and using the space
character as the delimiter. The ListLen( )
function performs the calculation. Once the number of words is
calculated, it is passed back to the calling template as a variable
named NumberOfWords
.
Example 19-3. Counting the Number of Words in a Block of Text
<CFSETTING ENABLECFOUTPUTONLY="Yes"> <!------------------------------------------------------------------------ ...
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.