Daniel
Daniel

Reputation: 35704

coldfusion ignore undefined variables

if I use

<cfoutput>#somevariable#</cfoutput>

and somevariable is not defined I get an error, how can I prevent the error from occourring? is there a simple way of implementing a conditional that doesn't require a bunch of extra lines?

Upvotes: 4

Views: 7188

Answers (2)

cfEngineers
cfEngineers

Reputation: 674

You can test for the variable

<cfoutput>
    <cfif isDefined("somevariable")>
        #somevariable#
    <cfelse>
        handle default scenario here
    </cfif>
</cfoutput>

or you could use inline conditional

<cfoutput>
    #IIF(isDefined("somevariable"),de(somevariable),de(""))#
</cfoutput>

Upvotes: 1

Todd Sharp
Todd Sharp

Reputation: 3355

<cfparam name="somevariable" default="" />

If you're on cf 9 you can use a ternary operation, but cfparam is more 'best practicey'.

#isDefined("somevariable") ? somevariable : 'default string'#

Upvotes: 11

Related Questions