更新2:
这里有一种更糟糕的方式。警告:以下是未记录的功能(但仍然很酷):
假设自定义标记返回如下值:
<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>
因此,标签的result属性需要一个变量名,该变量名将被设置为调用者。现在,通过以下方法,我们可以通过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>
更新:
下面的解决方案可能会导致意外的副作用。如果你的自定义标签返回一个值,由于标签是从组件中调用的,这个返回变量会被放入组件的变量作用域而不是调用模板的变量作用域中,因此您将无法访问它。当然,如果你要返回一个值,你应该考虑使用 CFC(如我上面所评论的)来代替自定义标签,所以请自行决定是否使用。
那么这种方法怎么样(改编自 Jake 的方法):
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>