Specifying tag attributes in a structure

This post is more than 2 years old.

Yesterday I was reading an article on Ben Forta's blog, Look, No Datasource, where he described how in ColdFusion 9, we can specify a default datasource at the application level that can then be used with all tags that use a datasource attribute. So instead of doing:

<cfquery name="getShots" datasource="#application.dsn#">

we can instead just do:

<cfquery name="getShots">

This is cool and all, but a reader commented that it would be nice if we could also supply default mail settings at the server level. I agree with him, it would be nice. Things like datasources, mail settings, etc, are typically high level things that individual tags should not need to worry about.

It occurred to me that he may not be aware of a feature, added in ColdFusion 8, which kind of allows for this right now. For a long time custom tags have supported an attributeCollection argument. This is a structure that acts like passed in arguments. So if a custom tag takes two arguments, num1 and num2, I could actually pass them in like so:

<cfset s = {num1="2",num2="67"}> <cf_foo attributeCollection="#s#">

That's not a great example as it didn't save me any keystrokes, but I think you get the idea. ColdFusion 8 simply expanded this to built in tags. So taking the reader's comment about mail, you could, if you wanted, do this in your Application.cfc file:

<cfset application.mail = {server="",username="mail",password="pass"}>

and then pass it to your cfmail tags:

<cfmail to="some@where.com" from="admin@foo.com" subject="Your Email" attributeCollection="#application.mail#"> foo </cfmail>

Ok, so again, there isn't a huge savings in keystrokes, but it does allow you to change your mail tags from one central structure. Mail server doesn't require a password anymore? Just remove it from the struct. Want to supply a failto attribute? Add it to the struct and every cfmail tag uses the struct will be updated.

I've got to be honest and say that I've not yet used this in production (mainly because I keep forgetting about it!) but it's pretty powerful stuff. Anyone out there using it?

Raymond Camden's Picture

About Raymond Camden

Raymond is a developer advocate for HERE Technologies. He focuses on JavaScript, serverless and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA https://www.raymondcamden.com