CFModule Template Attribute Error Message In Adobe ColdFusion
Yesterday, in an exploration of ColdFusion custom tags in Adobe ColdFusion, I ran into an issue with the template attribute of the cfmodule tag. Unlike other attributes, the template attribute cannot be passed into the cfmodule tag as part of the attributeCollection mechanics. This behavior is documented on Adobe site:
Note: This attribute functions differently from the
attributeCollectionattribute that is supported by most other tags. You must specify thenameandtemplateattributes as directcfmoduletag attributes, not in theattributeCollectionstructure.
I didn't know about this (or had forgotten); and wanted to write this blog post as a note to self, especially since the error message I was receiving was cryptic and didn't point me to the underlying cause of the error.
To demonstrate, I have a noop.cfm tag that does nothing. And I'm trying to invoke it via the attributeCollection:
<cfscript>
attrs = {
template: "./noop.cfm"
};
// ADOBE COLDFUSION (documented): Unlike other tags, you MUST pass name/template
// attributes as explicit attributes to the CFModule tag. They cannot be collected as
// passed as a group. This does not apply to Lucee CFML.
cfmodule( attributeCollection = attrs ) {
writeOutput( "Tag body here...." );
}
</cfscript>
If I run this CFML code in Adobe ColdFusion, I get the following error:
Type:
java.lang.NullPointerExceptionMessage: Cannot invoke "coldfusion.runtime.NeoPageContext.pushBody()" because "this.tagPageContext" is null.
Stacktrace:
java.lang.NullPointerException: Cannot invoke "coldfusion.runtime.NeoPageContext.pushBody()" because "this.tagPageContext" is null at coldfusion.tagext.lang.ModuleTag.doAfterBody(ModuleTag.java:391)
As you can see, the error message provides no meaningful insight about the template attribute. But now, when I Google it next time, I'm hoping that this post will come up.
To fix this, all I have to do is explicitly provide the template attribute even if I'm going to use the attributeCollection for subsequent attributes:
<cfscript>
attrs = {
hello: "world",
foo: "bar"
};
// Always provide `template` explicitly!
cfmodule(
template = "./noop.cfm",
attributeCollection = attrs
) {
writeOutput( "Tag body here...." );
}
</cfscript>
As a final note, Lucee CFML doesn't have an issue with this.
Want to use code from this post? Check out the license.
Reader Comments
Post A Comment — ❤️ I'd Love To Hear From You! ❤️
Post a Comment →