coldfusion ignore undefined variables
if I use
<cfoutpu开发者_开发技巧t>#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'#
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>
精彩评论