Setting Global, Default Tag Attributes With "this.tag" In Lucee CFML 5.3.7.47
In Lucee CFML, you can assign global, default tag attributes using the this.tag object in the Application.cfc ColdFusion component. I've never actually used this before; and, I've seen the technique mentioned both with and without the cf-prefix. As such, I wasn't sure what the official approach was; and, I wanted to try it out for myself in Lucee CFML 5.3.7.47. It seems that both syntax approaches work.
To test this, I created an Application.cfc that randomly created one of two different this.tag assignments. First, I tried it with a simple tag like <CFDump>:
component
output = false
hint = "I define the application settings and event-handlers."
{
this.name = "TestingGlobalTagAttributes";
this.applicationTimeout = createTimeSpan( 0, 0, 10, 0 );
this.sessionManagement = false;
this.tag = {};
// Randomly select one style of global tag attribute assignment (50/50 chance).
if ( randRange( 0, 1 ) ) {
this.tag.dump = {
label: "Set Using DUMP",
var: "Set using DUMP",
format: "text",
abort: false
};
} else {
this.tag.cfdump = {
label: "Set Using CFDUMP",
var: "Set using CFDUMP",
format: "simple",
abort: true
};
}
/**
* I process the incoming request.
*/
public void function onRequest() {
render( "<cfdump>" );
render( "This should not render if ABORT was default." );
}
}
As you can see, with the randRange(), 50% of the requests will get default values using this assignment:
this.tag.dump
And, 50% of the requests will get default values using this assignment (note the cf prefix):
this.tag.cfdump
Now, when I run this page a bunch of times, here's the composite output that I get:
As you can see, the global, default tag attributes were properly applied in both cases (with and without the cf prefix).
CAUTION:
CFDumpanddump()are not the same (at least in this context). And, as Partap Davis mentions in a Lucee Dev thread, the same divergence also applies toCFLocationandlocation(). So, take caution when using the function-based implementations that are available for several of the native ColdFusion tags.
Since the CFDump tag is relative simple, I wanted to try one more experiment with a complex tag like CFDocument:
component
output = false
hint = "I define the application settings and event-handlers."
{
this.name = "TestingGlobalTagAttributes2";
this.applicationTimeout = createTimeSpan( 0, 0, 10, 0 );
this.sessionManagement = false;
this.tag = {};
// Randomly select one style of global tag attribute assignment (50/50 chance).
if ( randRange( 0, 1 ) ) {
this.tag.document = {
format: "pdf",
pagetype: "custom",
pagewidth: "3",
pageheight: "1",
margin: {
top: 0,
right: 0,
bottom: 0,
left: 0
}
};
} else {
this.tag.cfdocument = {
format: "pdf",
pagetype: "custom",
pagewidth: "6",
pageheight: "2"
};
}
/**
* I process the incoming request.
*/
public void function onRequest() {
document {
echo( "This is my PDF!" );
}
}
}
This is the same exact approach in that half of the requests use the assignment:
this.tag.document
... and half of the requests use this assignment (note the cf prefix):
this.tag.cfdocument
And, when we run this ColdFusion page a number of times, we get the following composite output:
As you can see, the global, default tag attributes were properly applied in both cases (with and without the cf prefix).
So, it seems that the global, default tag attribute assignments work both with and without the cf prefix in Lucee CFML. That said, I think I prefer them without the cf prefix because this aligns more closely with how I invoke those same tags in a CFScript context.
Want to use code from this post? Check out the license.
Reader Comments