用于文件自定义的自定义 CFInclude
Custom CFInclude for file customization
我们的代码库包含大量以下示例,因为我们允许根据客户的个人需求自定义许多基页。
<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! --->
我们的代码库包含大量以下示例,因为我们允许根据客户的个人需求自定义许多基页。
<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! --->