Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Is it possible to call CF functions dymanically?

Contributor ,
Oct 15, 2008 Oct 15, 2008
Is it possible to call a ColdFusion function dynamically? For example you might have a date variable: myDate that you want to apply a ColdFusion date function to. However, the function itself needs to be dynamic because you might want to call "dateFormat", "timeFormat", "dateAdd", etc.

Is there a way to evaluate an expression like this?

<!-- defined the function to call and encapsulate the dynamic variable name with brackets -->
<cfset functionToCall = "DateFormat([myVariable],'MM/DD/YYYY')">
<cfset myDate = CreateODBCDate('03/19/1977')>

<!-- now replace the bracketed name with the actual variable data (myDate) -->
<cfset fullString = ReplaceNoCase(functionToCall,'[myVariable]','#myDate#','ALL')>

<!-- display the output -->
<cfoutput>
#evaluate(fullString )#
</cfoutput>

What I get here is an error message because there are invalid characters in "fullString". When I simply cfoutput fullString the text it produces looks perfect, its just that I need to find a way to evaluate it.

Any ideas?

Thanks for taking the time to help.
TOPICS
Advanced techniques
415
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 15, 2008 Oct 15, 2008
presumably, which function you need to use depends on some condition...
so you can just use a <cfif> or <cfswitch> block to check the condition
and use appropriate function...

you can even encapsulate it into your own function that uses built-in cf
functions depending on some other argument passed.
e.g:
<cffunction name="myveryowndatefunction">
<cfargument name="mydate" required="yes">
<cfargument name='myotherargument" required="yes">
<cfif arguments.myotherargument eq something>
<cfreturn use_one_cf_function>
<cfelseif ...>
<cfreturn use_other_cf_function
<cfelse>
<cfreturn use_yet_another_cf_function>
</cfif>
</cffunction>


Azadi Saryev
Sabai-dee.com
http://www.sabai-dee.com/
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Oct 15, 2008 Oct 15, 2008
LATEST
Hi,

You need to feed something to evaluate that is executable, so you should include something like "x= {dynamic stuff}" with "x' being a variable.

cheers,
fober

<cfset myDate = "2008-10-15 14:21:37">

<cfset functionToCallS = "DateFormat([myVariable],'MM/DD/YY')">
<cfset functionToCallL = "DateFormat([myVariable],'MMM/DD/YYYY/HH:mm')">

<cfset targetvar = "shortdate">
<cfset x= evaluate("#targetvar# =" & ReplaceNoCase(functionToCallS,"[myVariable]","'#myDate#'","ALL")) >

<cfset targetvar = "longdate">
<cfset x= evaluate("#targetvar# =" & ReplaceNoCase(functionToCallL,"[myVariable]","'#myDate#'","ALL")) >


<cfoutput>
VAR1(): #shortdate#<br>
VAR2(): #longdate#<br>
</cfoutput>

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources