我可以在cfscript中调用自定义标签吗?

rya*_*rns 8 coldfusion coldfusion-9

例如,如果我有一个类似<cf_AppSwitch action="Check">我的假设的自定义标签AppSwitch(action="Check"),但我不确定CF可以将其解析为自定义标签.

我能想到的另一个解决方案是编写一个包装函数并调用我的自定义标记,但这感觉多余.

似乎我过于简单化了一个更复杂的问题,所以任何见解都会受到赞赏(甚至为什么不支持/不应该支持).

Tod*_*arp 11

更新2:

这是一个更糟糕的屁股(或者是糟糕的asser?)方式.警告:下面没有文档的功能(但仍然很酷):

假设自定义标记返回如下值:

<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>
Run Code Online (Sandbox Code Playgroud)

因此,标记的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>
Run Code Online (Sandbox Code Playgroud)

更新:

以下解决方案可能会导致不良副作用.如果您的自定义标记返回一个值,您将无法访问它,因为从组件调用了标记,返回变量将被放入组件的变量范围,而不是调用模板.当然,如果你要返回一个值,你应该使用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>
Run Code Online (Sandbox Code Playgroud)

echo.cfm:

<cfif thisTag.executionMode eq "start">
    <cfparam name="attributes.name" default="Dude" />
    <cfoutput>Hello, #attributes.name#!!</cfoutput>
</cfif>
Run Code Online (Sandbox Code Playgroud)

time.cfm:

<cfif thisTag.executionMode eq "start">
    <cfoutput>It is now #now()#.</cfoutput>
</cfif>
Run Code Online (Sandbox Code Playgroud)

index.cfm:

<cfscript>
proxy = new CustomTagProxy();
echoTest = proxy.echo(name="Todd");
timeTest = proxy.time();

writeOutput(echoTest);
writeOutput("<br />");
writeOutput(timeTest); 
</cfscript>
Run Code Online (Sandbox Code Playgroud)


Jak*_*sel 7

假设您正在使用Adobe CF,不幸的是答案是否定的.您必须编写基于CFML的包装函数.例如:

<cffunction name="myCustomTag">
  <cfset var returnVal = "">
  <cfsavecontent variable="returnVal"><cf_myCustomTag attributeCollection=arguments></cfsavecontent>
  <cfreturn returnVal>
</cffunction>

<cfscript>
 myCustomTag(a="b");
</cfscript>
Run Code Online (Sandbox Code Playgroud)

现在,如果你正在使用Railo,您可以使用CFSCRIPT相当于<cfmodule>标签:

<cfscript>
    module name="myCustomTag";
</cfscript>
Run Code Online (Sandbox Code Playgroud)

  • 有些人认为标签适用于基于UI的操作,组件更适合于模型/服务.因此,很少有人需要做这样的事情. (3认同)