Skip to main content
Ben Nadel
On User Experience (UX) Design, JavaScript, ColdFusion, Node.js, Life, and Love.

The Power Of Cascading Tag Attributes In Lucee CFML 5.3.7.43

By Ben Nadel on
Tags: ColdFusion

The other week, when I was looking at using Postmark Tags to group related transactional emails in Lucee CFML, I used a ColdFusion Struct to apply common CFMail tag attributes to all of my outbound emails. This attributeCollection feature has been around in ColdFusion since forever. However, in more modern times, ColdFusion has also allowed us to define any tag's base attributes right within the Application.cfc file. Cascading tag attributes are super powerful; and, since I've never really talked about them before, I thought it would be worth a quick demo in Lucee CFML 5.3.7.43.

In ColdFusion, there are three places that we can define tag attributes:

  1. Using this.tag in the Application.cfc.
  2. Using the attributeCollection on a given tag instance.
  3. As individual attributes on a given tag instance.

You can think of this.tag, in the Application.cfc, as defining an implicit attributeCollection that gets applied automatically to all ColdFusion tags of a given type. We can then augment and override that implicit attributeCollection with an explicit attributeCollection on an individual tag; which can, in turn, be augmented and overridden by individual tag attributes on a given tag.

To see this in action, I've put together an Application.cfc ColdFusion application framework file that sends out an email. The attributes for the CFMail tag are going to be defined using all three attribute locations: this.tag, attributeCollection, and - finally - individual tag attributes:

component
	output = false
	hint = "I define the application settings and event-handlers."
	{

	// Define the core application settings.
	this.name = "TagAttributesDemo";
	this.applicationTimeout = createTimeSpan( 0, 0, 10, 0 );
	this.sessionManagement = false;

	// CASCADE 1: Within the Application.cfc, we can define BASE ATTRIBUTES that will be
	// applied to all tags with the given names. In this case, we're defining the base
	// attributes for the CFMail tag, which will automatically be applied to all outbound
	// email in the application.
	// --
	// NOTE: These values can all be overridden within an individual tag instance.
	this.tag = {
		mail: {
			server: "smtp.postmarkapp.com",
			port: "2525",
			username: "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
			password: "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
			mailerid: "TagAttributeDemoInLuceeCFML"
		}
	};


	/**
	* I get called before each inbound application request.
	*/
	public void function onRequestStart() {

		// CASCADE 2: Building on top of the base CFMail attributes above, we can also
		// build up a local collection of tag-attribute to be applied to a given tag
		// instance below (using the "attributeCollection" mechanics).
		var overrideAttributes = {
			async: false,
			type: "html",
			wraptext: 900,
			charset: "utf-8",
			from: "ben@bennadel.com"
		};

		// CASCADE 3: And, of course, at each individual tag level, we can override any
		// of the attributes defined above while also providing new tag attributes.
		mail
			to = "ben+demo@bennadel.com"
			subject = "Good morning, check out these cascading tag attributes!"
			attributeCollection = overrideAttributes
			{

			echo( "<p>" );
			echo( "Hello there, good sir, how are you doing on this fine morning." );
			echo( "</p>" );

		}

	}

}

All three of the CFMail tag attribute specifications in this file coalesce into the single CFMail tag instance. In this particular demo, I don't have any conflicting tag attributes; but, if I did, the "lower level" attribute wins. Meaning, the attributeCollection will override conflicting attributes from the this.tag specification; and, individual tag attributes within the CFMail tag will override conflicting tags in the attributeCollection specification.

Cascading tag attributes is just one more way in which ColdFusion creates outstanding developer ergonomics. What an amazing language!



Reader Comments

I had no idea this.tag was a thing! Mind blown! Super useful! Thanks for this! Now, I'm wondering if this is just a Lucee thing or whether it also works in ACF.

Reply to this Comment

@Chris,

Ha ha, glad to be able to share something exciting :D I know that the attributeCollection definitely works in Adobe ColdFusion; but, I am not sure about the this.tag stuff in the Application.cfc. I'm trying to Google it, but I am not finding much. That latter one may be Lucee CFML only.

Reply to this Comment

Post A Comment

You — Get Out Of My Dreams, Get Into My Blog
Live in the Now
Oops!
NEW: Some basic markdown formatting is now supported: bold, italic, blockquotes, lists, fenced code-blocks. Read more about markdown syntax »
Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.