
Page 1 of 1 When working in ColdFusion, you have two basic ways of coding: ColdFusion tags and CFScript. Both are legitimate and both provide certain advantages. With ColdFusion tags, you have a full arsenal of functionality available. With CFScript, you have more basic script operations available, such as looping, setting variables, conditions, and other more mundane tasks. Frequently when writing scripts, however, you need some functionality that is only available to a ColdFusion tag. The more obvious solution is to close the </cfscript> tag, execute your tag, then re-open the <cfscript> tag to resume your coding. We'll talk about another way to approach this situation—create functions that mimic CF tags.
ColdFusion has user-defined functions available for use within your ColdFusion pages—whether you are scripting or creating code with tags. Some things that make sense from a scripting point of view involve loops, conditions, and string manipulation. However, sometimes within these blocks of code you might need the power of a ColdFusion tag, such as <cfmail>, <cffile>, or even a simple <cfparam>. Through the use of a user-defined function, we can create proxies into any one of these tags that can be accessed from your <cfscript> tag.
The simplest example is a <cfparam> tag. Looking at the tag in its most basic form, it is doing two things: checking for the existence of a variable, and assigning a default value if the variable does not exist yet. We can do the following to set up a default value for an incoming form variable that may or may not be posted:
<cfparam name="form.firstName" default="" />
This is simply saying "if a form element named 'firstname' doesn't exist, give it a blank value." This is useful to prevent those dreaded undefined variable error messages if the page is hit without the form value it expects. From a <cfscript> point of view, you might do the following to mimic this:
<cfscript>
if( Not IsDefined("form.firstname") ) {
form.firstName = "";
}
</cfscript>
It's not too cumbersome, but it is not as simple as using a <cfparam>. We can create a custom cfparam function to take the place of the tag:
<cffunction name="cfparam">
<cfargument name="variable" required="true" />
<cfargument name="default" required="true" />
<cfif Not isDefined(arguments.variable)>
<cfreturn "" />
<cfelse>
<cfreturn Evaluate(arguments.variable) />
</cfif>
</cffunction>
Now we can use the function to use "cfparam" within our scripts:
<cfscript>
cfparam("form.firstname", "");
cfparam("form.lastName", "");
</cfscript>
This simple example can be expanded to use more complex tags, such as a <cfmail> tag. We'll create a simple example of a <cfmail> function that uses the most frequent attributes:
<cffunction name="cfmail">
<cfargument name="to" required="true" />
<cfargument name="from" required="true" />
<cfargument name="subject" required="true" />
<cfargument name="body" required="true" />
<cfmail to="#arguments.to#"
from="#arguments.from#"
subject="#arguments.subject"#>
#arguments.body#
</cfmail>
<cfreturn>
</cffunction>
Now, within the script body, we can execute the sending of email:
<cfscript>
body="Hello. I'm a message being sent through a script.";
cfmail(to="you@yourdomain.com",
from="me@mydomain.com",
subject="This is a sample message",
body="#body#");
</cfscript>
Finally, we'll do a simple <cfdump> tag for debugging, which is always useful when programming.
<cffunction name="cfdump">
<cfargument name="theVariable">
<cfdump var="#theVariable#">
</cffunction>
Call it like this:
<cfscript>
variables.firstname="Tom";
variables.lastname="Muck";
cfdump(variables);
</cfscript>
Note that I did not use the <cfdump> argument name "var" because it is a keyword in ColdFusion, but the result is the same.
This article showed a simple concept of using ColdFusion user-defined functions to mimic tag functionality. The <cfscript> tag has a few limitations, most of which revolve around the inability to execute ColdFusion tags from within a script block, but using a few carefully devised functions you can use <cfscript> with tags too.
Page 1 of 1 1
Keywords
cfscript,cffunction,tags,coldfusion