Using Coldfusion tags inside CFscript - AttributeCollection makes it easy

Author: sandeepp  |  Category: Coldfusion, attributeCollection  |  Comments (3)  |  Add Comment

Coldfusion 8 release added support for attributeCollection in Coldfusion tags. This feature makes it much more easy to wrap CF Tags as function and use them inside cfscript.

Now I am sure most Coldfusion users must already have created wrapper functions for CF tags ( similar to this ). One problem with this approach is that either you use only few attributes which the function accepts or write complex logic to check what attributes have been passed to function and use them. This makes it a little difficult to make use of all the functionality which a tag provides.

Now with attributeCollection all these issues are solved. You can use same wrapper function for a given tag and make use of all different functionality which that tag provides (ex. use cfzip function to list all entries of a zip file or create new file or delete one) .  All that you need to do is write a wrapper function (for a given tag) which accepts a struct as argument. Pass this struct as attributeCollection to the tag, and that’s it!!, You are done. To use this inside cfscript, create a struct for all the attributes you want to pass this tag. Call the wrapper function and pass this struct.

Here’s sample code to show this.

<cfscript>

args.from=”#fromemail#”;
args.subject=”#subject#”;
args.server=”#mailserver#”;
args.port=”#port#”;

for(i=1;i<ArrayLen(tolist);i++)
{
args.to=”#toList[i]#”; //change email address of to field
args.subject=”Hello #tolist[i]#,” & args.subject; // personalize subject for each user
cfmail(args); //call function with changed arguments
}
</cfscript>

Wrapper Function for cfmail tag

<cffunction name=”cfmail” output=”no”>
<cfargument name=”arguments” type=”struct” required=”yes”>

<cfmail attributecollection=”#arguments#” >
</cfmail>
</cffunction>

As you see in above code sample, using attributeCollection code required to wrap CF tag inside function is reduced to a line now.

Note: Since attributeCollection support in Tags was added in Coldfusion8, above code will only work on Coldfusion8 and not on previous versions.

Coldfusion Server Monitor - Aliasing

Author: sandeepp  |  Category: Coldfusion, servermonitoring  |  Comments (6)  |  Add Comment

What is Server Monitor Aliasing?

Aliasing is a feature in Coldfusion Server Monitoring to monitor CF applications based on frameworks(like MachII, Model Glue, Fusebox etc.)

All these framework applications have a controller (eg. index.cfm) and all requests go through that template, and based on certain action parameters(eg. event=xyz) it then executes different templates.

Previously if you monitor such applications, reports will be generated only for index.cfm, making it difficult to debug or analyze performance of these apps.

Aliasing helps to monitor such applications and get statistics(like request, memory statistics etc.) based on value of action parameters.
Thus, index.cfm?event=abc and index.cfm?event=xyz will have separate reports generated for them.

Also this can be used to monitor individual templates based on different request parameters.

Steps to setup Aliasing?

  • Open Server monitor and click settings icon.
  • Click Aliasing tab in settings dialog.
  • Click on add button and fill required fields (as shown below)

Template Path = C:\ColdFusionScorpio\wwwroot\cart\index.cfm
Alias = homepage
Action Parameter = event ( or whatever is action parameter for framework you use, also it can be comma     separated list of any parameters accepted by cfm page).

  • Click OK and you are done.

Now you can view reports for this added alias  in statistics tab.

There will be new entry for every unique value of action parameter. something like -

homepage?event=val1

homepage?event=val2 and so on.

Once you have these reports it becomes much more easy to tune performance of your application.