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?
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?
<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'#
structkeyexists( scope, "somevariable" )
rather than isdefined
. It's quicker to search only the scope in which you expect the variable to be defined, and it prevents CF from retrieving a variable from a different scope. If you do use isdefined
, at least provide the scope with the variable (which the OP should have done as well).
Jul 8, 2011 at 19:54
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>