Skip to main content
Inspiring
June 1, 2010
Answered

return a value from a custom tag ?

  • June 1, 2010
  • 2 replies
  • 2349 views

For example, if I have a custom tag that calcs area, eg

<cf_calcArea width="2" height="3">

How do I return the answer so I can put it in a variable ?

<cfset variables.answer = <cf_calcArea width="2" height="3">>

This topic has been closed for replies.
Correct answer -__cfSearching__-

You could add an attribute for the result variable name.  Then inside the custom tag, create that variable in the CALLER scope.  See #4

http://www.coldfusionjedi.com/index.cfm/2006/8/17/ColdFusion-Custom-Tag-Tips

2 replies

ilssac
Inspiring
June 2, 2010

Dax Trajero wrote:

<cfset variables.answer = <cf_calcArea width="2" height="3">>

I would like to point out, that if this is the type of functionality you are looking for, then custom tag is probably the wrong feature.  That syntax speaks more to a user defined function (UDF) or component (CFC) rather then a custom tag.

Custom tags are geared for generating modular output rather then returning a calculated value to the calling code, even though you have leared how they can through the caller scope.

Inspiring
June 2, 2010

(scurries off to look up UDF's and CFC's )

it's working Ian - is it worth changing ?

I was a little worried about the scope of the returned variable, so I immediately CFSET variables.Result = tagResult>

Oh, while I'm on, whats the best way to format a numeric so it has two decimal places ? eg.

variable.lineTotal = 3

I want it to display 3.00

Is numberformat the best bet ?

ilssac
Inspiring
June 2, 2010

Dax Trajero wrote:

it's working Ian - is it worth changing ?

Can't say, but using logic in non-standard ways can make an applicaiton much more difficult to maintain in the future when it's time to change something.

Dax Trajero wrote:

variable.lineTotal = 3

I want it to display 3.00

Is numberformat the best bet ?

That is what the numberFormat() function is designed to do.

-__cfSearching__-Correct answer
Inspiring
June 1, 2010

You could add an attribute for the result variable name.  Then inside the custom tag, create that variable in the CALLER scope.  See #4

http://www.coldfusionjedi.com/index.cfm/2006/8/17/ColdFusion-Custom-Tag-Tips

Inspiring
June 1, 2010

ah, the caller scope - first time I've come across it.

I googled the topic several times and nobody mentioned the caller scope!! Sheesh!

Big thanks cfSearching!