gpt4 book ai didi

coldfusion - 用于文件自定义的自定义 CFInclude

转载 作者:行者123 更新时间:2023-12-01 11:14:46 25 4
gpt4 key购买 nike

我们的代码库包含大量以下示例,因为我们允许根据客户的个人需求自定义许多基页。

<cfif fileExists("/custom/someFile.cfm")>
<cfinclude template="/custom/someFile.cfm" />
<cfelse>
<cfinclude template="someFile.cfm" />
</cfif>

我想创建一个自定义 CF 标签,将其样板化为一个简单的 <cf_custominclude template="someFile.cfm" /> ,但是我遇到了这样一个事实,即自定义标签实际上是黑盒,因此它们不会引入标签开始之前存在的局部变量,而且我无法引用由于标签而创建的任何变量从导入文件。

例如

<!--- This is able to use someVar --->
<!--- Pulls in some variable named "steve" --->
<cfinclude template="someFile.cfm" />
<cfdump var="#steve#" /> <!--- This is valid, however... --->

<!--- someVar is undefined for this --->
<!--- Pulls in steve2 --->
<cf_custominclude template="someFile.cfm" />
<cfdump var="#steve2#" /> <!--- This isn't valid as steve2 is undefined. --->

是否有解决此问题的方法,或者我应该利用其他一些语言功能来实现我的目标?

最佳答案

好吧,我完全质疑这样做,但我知道我们有时都会收到我们必须处理的代码,并且努力让人们重构。

这应该可以满足您的要求。需要注意的一件重要事情是,您需要确保您的自定义标签有一个结束符,否则它将不起作用!只需使用简化的关闭,就像上面那样:

<cf_custominclude template="someFile.cfm" />

这应该可以解决问题,称它为您拥有它:custominclude.cfm

<!--- executes at start of tag --->
<cfif thisTag.executionMode eq 'Start'>
<!--- store a list of keys we don't want to copy, prior to including template --->
<cfset thisTag.currentKeys = structKeyList(variables)>
<!--- control var to see if we even should bother copying scopes --->
<cfset thisTag.includedTemplate = false>
<!--- standard include here --->
<cfif fileExists(expandPath(attributes.template))>
<cfinclude template="#attributes.template#">
<!--- set control var / flag to copy scopes at close of tag --->
<cfset thisTag.includedTemplate = true>
</cfif>
</cfif>
<!--- executes at closing of tag --->
<cfif thisTag.executionMode eq 'End'>
<!--- if control var / flag set to copy scopes --->
<cfif thisTag.includedTemplate>
<!--- only copy vars created in the included page --->
<cfloop list="#structKeyList(variables)#" index="var">
<cfif not listFindNoCase(thisTag.currentKeys, var)>
<!--- copy from include into caller scope --->
<cfset caller[var] = variables[var]>
</cfif>
</cfloop>
</cfif>
</cfif>

我测试了它,它工作正常,嵌套也应该工作正常。祝你好运!

<!--- Pulls in steve2 var from include --->
<cf_custominclude template="someFile.cfm" />
<cfdump var="#steve2#" /> <!--- works! --->

关于coldfusion - 用于文件自定义的自定义 CFInclude,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53836846/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com