Skip to main content
May 19, 2011
Answered

How can you use a conditional statement inside a <cfset>

  • May 19, 2011
  • 3 replies
  • 1884 views

I want to write a line of code that basically says:

<cfset foo = 0 <cfif foo2 IS NOT "">+ #foo3#</cfif> >

I am getting an error. is this possible?

And if it is possible, can you add multiple conditions?

Thanks!

    This topic has been closed for replies.
    Correct answer Adam Cameron.

    No, you can't do this. You cannot use CFML to built another CFML statement at runtime.  All the CFML needs to be compiled before it's executed, so seen in that light you can probably see how what you're trying can't possibly work.

    You need to have the code all present and correct at compile time, so it can be compiled, and then it can be executed.

    So something like this:

    <cfif foo2 IS "">

    <cfset foo = 0>

    <cfelse>

    <cfset foo = foo3>

    </cfif>

    Obviously this is just your example, and your actual requirement is different, but the key thing here is to understand the difference between compile-time and runtime.

    --

    Adam

    3 replies

    Community Expert
    May 19, 2011

    You cannot use one CFML tag nested within another CFML tag's actual tags. For example, this would be invalid (assuming that you had tags called cffoo and cfbar in CFML):

    <cffoo <cfbar></cfbar>>...</cffoo>

    while this would be valid:

    <cffoo> <cfbar></cfbar></cffoo>

    To answer your direct question, if you want to embed a conditional statement inside CFSET, use the IIf function:

    <cfset foo = Iif(...)>

    Dave Watts, CTO, Fig Leaf Software

    http://www.figleaf.com/

    http://training.figleaf.com/

    Dave Watts, Eidolon LLC
    Participating Frequently
    May 19, 2011

    Meh - IIF is so CF5...

    Use a ternary operation if you're on CF9.

    <cfset foo = 0 <cfif foo2 IS NOT "">+ #foo3#</cfif> >

    can be:

    <cfset foo = len(trim(foo2)) ? foo2 : 0 />

    Owainnorth
    Inspiring
    May 19, 2011

    Damn you Cfsilence, I was going to suggest a ternary operator and make myself look awesome.

    Curses.

    ilssac
    Inspiring
    May 19, 2011

    NO THAT IS NOT POSSIBLE.

    And I, for one, really can't see what you are trying to do.

    I can only guess that you are trying a really backhanded way to do some type of boolean?

    What is wrong with:

    <cfset foo = 0>

    <cfif foo2 IS NOT "">

         <cfset foo = foo + foo3>

    </cfif>

    Seeing Adam's example.  You may also want to explore the <cfparam...> tag.

    <cfparam name="foo" default="0">

    <cfif foo2 IS NOT "">

         <cfset foo = foo + foo3>

    </cfif>

    Adam Cameron.Correct answer
    Inspiring
    May 19, 2011

    No, you can't do this. You cannot use CFML to built another CFML statement at runtime.  All the CFML needs to be compiled before it's executed, so seen in that light you can probably see how what you're trying can't possibly work.

    You need to have the code all present and correct at compile time, so it can be compiled, and then it can be executed.

    So something like this:

    <cfif foo2 IS "">

    <cfset foo = 0>

    <cfelse>

    <cfset foo = foo3>

    </cfif>

    Obviously this is just your example, and your actual requirement is different, but the key thing here is to understand the difference between compile-time and runtime.

    --

    Adam