Is it possible to call a custom tag in cfscript?
For example, if I have my own tag like <cf_AppSwitch action="Check">
, then my assumption would be similar to AppSwitch(action="Check")
, but I'm not sure that CF can resolve this as a custom tag.
Another solution I can think of would be to write a wrapper function and call my custom tag, but that seems redundant.
It seems like I'm simplifying a much more complicated problem, so any understanding will be appreciated (even if it is not supported / not supported).
Assuming you are using Adobe CF, unfortunately, the answer is no. You must write a wrapper function based on CFML. For instance:
<cffunction name="myCustomTag"> <cfset var returnVal = ""> <cfsavecontent variable="returnVal"><cf_myCustomTag attributeCollection=arguments></cfsavecontent> <cfreturn returnVal> </cffunction> <cfscript> myCustomTag(a="b"); </cfscript>
Now, if you use Railo, you can use cfscript equivalent to the <cfmodule>
:
<cfscript> module name="myCustomTag"; </cfscript>
Update 2:
There is even an ass ass (or is this a bad question?). Warning: undocumented features below (but still cool):
Suppose a custom tag returns this value:
<cfif thisTag.executionMode eq "start"> <cfparam name="attributes.name" default="Dude" /> <cfparam name="attributes.result" type="variablename" default="result" /> <cfset caller[attributes.result] = "Hello, " & attributes.name & "!!" /> </cfif>
Thus, the tag result attribute expects the variable name to be set in the caller. Now, using the method below, we can access this result through cfscript.
<cfscript> test = createObject("java", "coldfusion.tagext.lang.ModuleTag"); test.setPageContext( getPageContext() ); test.setTemplatePath(expandPath('echo.cfm')); test.setAttributeCollection({name="Todd Sharp", result="testResult"}); test.doStartTag(); test.doEndTag(); test.releaseTag(); writeDump(testResult); </cfscript>
Update:
The solution below may cause an unwanted side effect. If your custom tag returns a value, you will not have access to it, since the tag is called from the component, the returned variable falls into the component variable area, not the caller. Of course, if you return a value, you should probably use CFC anyway (as I noted above), so use it at your own risk.
How about this approach (modified from Jake's):
CustomTagProxy.cfc:
<cfcomponent> <cffunction name="onMissingMethod" output="false"> <cfargument name="missingMethodName" type="string"/> <cfargument name="missingMethodArguments" type="struct"/> <cfset var returnVal = ""> <cfsavecontent variable="returnVal"><cfmodule template="#arguments.missingMethodName#.cfm" attributecollection="#arguments.missingMethodArguments#" /></cfsavecontent> <cfreturn returnVal> </cffunction> </cfcomponent>
echo.cfm:
<cfif thisTag.executionMode eq "start"> <cfparam name="attributes.name" default="Dude" /> <cfoutput>Hello, #attributes.name#!!</cfoutput> </cfif>
time.cfm:
<cfif thisTag.executionMode eq "start"> <cfoutput>It is now #now()#.</cfoutput> </cfif>
index.cfm:
<cfscript> proxy = new CustomTagProxy(); echoTest = proxy.echo(name="Todd"); timeTest = proxy.time(); writeOutput(echoTest); writeOutput("<br />"); writeOutput(timeTest); </cfscript>