我收到“ transaction_types”未定义错误,并且无法理解原因。
我有application.cfc:
<cffunction name="onRequest" >
<cfargument name="targetPage" type="String" required=true/>
<cfinclude template="header.cfm">
</cffunction>
header.cfm文件如下所示(在每个文件上都调用该头,并且根据用户所在的目录,有不同的子头):
<cfinclude template="#GetDirectoryFromPath(Arguments.targetPage)#subheader.cfm" />
我遇到问题的目录有两个文件,index.cfm和subheader.cfm
subheader.cfm,第一行
<cfset transaction_types = ["a", "b", "c"] />
index.cfm的一部分,我认为,问题可能是cflocation,但我不确定:
<cfif structKeyExists(url, "something") >
-- some database work is done here --
<cflocation url="index.cfm">
</cfif>
--further down on this page, transaction_types is used
我设置页面为上,认为在每次加载目录/index.cfm时都将定义transaction_types,因为应用程序文件总是先加载header.cfm,然后再加载directory / subheader.cfm,再加载directory / index.cfm。 cflocation是否绕过这个?
答案 0 :(得分:0)
您将在OnRequest中包含代码,该代码在Application.cfc的variables
范围内设置变量,然后稍后尝试在模板中引用。
cfc的变量范围通常(包括特殊情况下的Application.cfc在内)通常不会传递到作为请求的一部分而被调用的模板。
如果需要在Application.cfc OnRequest期间设置transaction_types
(是否在包含的模板中),然后在index.cfm中进行引用,则应在request
之类的范围内进行设置,然后在以后引用这样。
subheader.cfm
<cfset request.transaction_types = ["a", "b", "c"] />
然后在index.cfm代码中引用为request.transaction_types
。