Skip to main content
February 8, 2007
Answered

cfoutput from a variable that contains logic

  • February 8, 2007
  • 5 replies
  • 609 views
I currently have some logic stored in a table and when I try to run the code in a cfoutput It doesn't execute the logic as if it was inline.

<cfset middlename='2'>
<cfset var1="<cfif len(middlename) eq 1>#middlename#,<cfelse>#middlename#</cfif>">
<cfoutput>#var1#</cfoutput>

//this outputs
2,2
instead of 2, or 2
This topic has been closed for replies.
Correct answer Newsgroup_User
I currently have some logic stored in a table and when I try to run the
code in
a cfoutput It doesn't execute the logic as if it was inline.

<cfset middlename='2'>
<cfset var1="<cfif len(middlename) eq
1>#middlename#,<cfelse>#middlename#</cfif>">
<cfoutput>#var1#</cfoutput>

//this outputs
2,2
instead of 2, or 2

You are not allowed to nest tags inside the brackets of other tags like
that. If you look at the source in your browser you will that this is
actually outputting the entire code "<cfif len(middlename) eq
1>2,<cfelse>2</cfif>", but the browser is ignoring the tags <...> it
does not understand and not rendering that portion of the string. So
that all you see in the view port is 2,2.

To accomplish this example you could use the #iif()# function.

<cfset var1="#iif(len(middlename),DE('2,'),DE('2'))#">

But for your original question, to run code that is stored in a database
you would make use of the evaluate() and|or de() [delay evaluate]
functions. See the documentation for how these functions work.

5 replies

Inspiring
February 12, 2007
> I currently have some logic stored in a table and when I try to run the code in
> a cfoutput It doesn't execute the logic as if it was inline.

No because <cfoutput> does what it says on the box: it *outputs* it.

This question did the rounds of the fourms a couple of weeks ago, and there
was some discussion as to various approaches to effecting what you want to
achieve.

I recommend you do a search: there's no point re-treading the same ground.

--
Adam
Inspiring
February 12, 2007
You can variable values and stuff like that.
BKBK
Community Expert
Community Expert
February 9, 2007
> ... when I try to run the code in a cfoutput It doesn't execute the logic
> as if it was inline.


True. That is by design. If you require Coldfusion to output a string, it will de-fang it for tags. That is, it will ignore all words of the form <text>. Thus, Coldfusion de-fangs <cfif len(middlename) eq1>, <cfelse> and </cfif>, and what your code essentially does is:

<cfoutput>#middlename#,#middlename#</cfoutput>




Participant
February 12, 2007
Related to the subject, I come to ASK if it would be possible to have logic saved to a table for later execution.
The need behind the question is to save the user predefined logic on a table that then can be called and executed.
Newsgroup_UserCorrect answer
Inspiring
February 8, 2007
I currently have some logic stored in a table and when I try to run the
code in
a cfoutput It doesn't execute the logic as if it was inline.

<cfset middlename='2'>
<cfset var1="<cfif len(middlename) eq
1>#middlename#,<cfelse>#middlename#</cfif>">
<cfoutput>#var1#</cfoutput>

//this outputs
2,2
instead of 2, or 2

You are not allowed to nest tags inside the brackets of other tags like
that. If you look at the source in your browser you will that this is
actually outputting the entire code "<cfif len(middlename) eq
1>2,<cfelse>2</cfif>", but the browser is ignoring the tags <...> it
does not understand and not rendering that portion of the string. So
that all you see in the view port is 2,2.

To accomplish this example you could use the #iif()# function.

<cfset var1="#iif(len(middlename),DE('2,'),DE('2'))#">

But for your original question, to run code that is stored in a database
you would make use of the evaluate() and|or de() [delay evaluate]
functions. See the documentation for how these functions work.
Inspiring
February 8, 2007
why do you have <cfif> inside <cfset>???
just do:
<cfset middlename='2'>
<cfoutput><cfif len(middlename) eq 1>#middlename#,<cfelse>#middlename#</cfif></cfoutput>