Possible Bug/Problem When Using DE Inside Of An IIf Function

Tuesday, July 1st, 2008

I’ll start by saying that this post and the example below were developed after reading Be Careful Using “#” In ColdFusion DE() Expressions by Ben Nadel. In a response to that blog post, Ubqtous responded with a comment that revealed an undocumented (?) feature and alternative to using the IIF/DE combination.

If you haven’t already read Ben’s blog post on this topic - I recommend reading through it to understand the code example below.

I added inline comments - though terse, they pretty much sum things up. I hope someone finds this useful, or it saves someone possibly hours of frustration.

<!--- Typical IIf/DE --->
<cfoutput>
    #IIf(
        true EQ false,
        DE("Hello, world!"),
        DE("Goodbye, world!")
    )#
</cfoutput>

<!--- Alternative to IIf/DE --->
<cfoutput>
    #IIf(
        true EQ false,
        "'Hello, world!'",
        "'Goodbye, world!'"
    )#
</cfoutput>

<!--- Why not to use DE? --->
<cfset strA = "(530) 555-1212" />
<!--- Remember this is escaped, really: #985 --->
<cfset strB = "##985" />

<!--- Typical IIf/DE --->
<!---
    This will crash!
    DE doesn't take into account escaped #s.
    Instead, it treats #s as a start of an expression.
--->
<cfoutput>
    #IIf(
        Len(strB),
        DE(strA & "x" & strB),
        DE(strA)
    )#
</cfoutput>

<!--- Alternative to IIf/DE --->
<cfoutput>
    #IIf(
        Len(strB),
        "strA & 'x' & strB",
        "strA"
    )#
</cfoutput>