Friday, 30 July 2010

when cfparam wont cut it

Random a couple of weeks ago , that in all honesty I should have known


I use cfparam alot, its easy to use and does the Job, but as I have gotten older default values for variables have gotten a little bit more complex than yes/no/now().


So....... at one point I started to do stuff like this (not exactly like this or I would need a better job.)


<!--- set a var--->

<cfset myname =" 'Ted'">


<!--- set a default --->

<cfparam name="myName" default="#_SayMyName()#">


<!--- putput the var --->

<cfoutput>#myName#</cfoutput>


<!--- Function to set default--->

<cffunction name="_SayMyName" returntype="string" output="yes">

<cfreturn>

</cffunction>


Which looks fine and dandy. but then on the execution plan I started to notice that the Function (that held a database lookup in the RL example) the function always executed.


The reason for this, is simple #_SayMyName()# runs the function, there I have said it.


But the golden child that was cfparam now isn't the all problem solving tag as I had previously thought.


So to cut a long story short... the correct answer is thus.


<cfif>

<cfset myname =" _SayMyName()">

</cfif>


*Head bangs off desk*

0 comments:

Post a Comment