coldfusion 使用 include 拆分循环
coldfusion splitting a loop with an include
这是一些代码:
<!---this is identical in several programs --->
<cfset i = 0>
<cfoutput>
<cfloop array = "#colvalue#" index = "val">
<cfset i = i + 1>
<cfset fieldname = colarr[i]>
<cfset modnum = i%basenum>
<script>
putval2('#val#', '#i#')
</script>
<!--- stuff which is different in each program --->
</cfloop>
</cfoutput>
这工作正常。
我将相同的代码放入一个名为 putback.cfm 的单独模块中,并将其重写为:
<cfinclude template = "putback.cfm">
<!---stuff which is different in each program --->
</cfloop>
我收到错误消息 "No matching start tag for end tag [cfloop]"。
我认为包含意味着 "putback.cfm" 将被提取并转储到程序中的那个位置。但是由于它没有认识到结束标记 cfloop 与 putback.cfm 中的开始 cfloop 一起出现,显然它不像我所理解的那样透明。
任何人都可以解释为什么这不起作用 and/or 提供解决方法。
I thought the include meant that "putback.cfm" would be picked up and dumped into that spot in the program.
关闭,但仅在运行时放置和处理包含。由于表达式错误(由 CFML 解析器),您没有达到那个点。
it is not recognizing that the end tag cfloop goes with with beginning cfloop in putback.cfm
解析器运行您的 .cfm 模板以在执行任何代码(运行时)之前构建 AST。根据解析器,循环永远不会关闭,因为 include 是另一个 .cfm 模板,它是单独检查的,并且从未在其包含状态中被触及。
修复
<!--- stuff which is different in each program --->
是应该包含的部分。但是,这种编码习惯被认为是不好的,最终会导致 Spaghetti code as you require, reuse and depend on variables inside of the loop. Your include should not be aware of its surroundings. If you absolutely have to do it this way, at least use cfmodule 将变量传递给您的 include。
解决这个问题的正确方法是使用接口。您指定所需的内容,提供您拥有的内容,然后让 <!--- stuff which is different in each program --->
为该界面提供服务。此代码结构将基于 cfcomponent and its cffunctions, implementing cfinterface.
这是一些代码:
<!---this is identical in several programs --->
<cfset i = 0>
<cfoutput>
<cfloop array = "#colvalue#" index = "val">
<cfset i = i + 1>
<cfset fieldname = colarr[i]>
<cfset modnum = i%basenum>
<script>
putval2('#val#', '#i#')
</script>
<!--- stuff which is different in each program --->
</cfloop>
</cfoutput>
这工作正常。
我将相同的代码放入一个名为 putback.cfm 的单独模块中,并将其重写为:
<cfinclude template = "putback.cfm">
<!---stuff which is different in each program --->
</cfloop>
我收到错误消息 "No matching start tag for end tag [cfloop]"。
我认为包含意味着 "putback.cfm" 将被提取并转储到程序中的那个位置。但是由于它没有认识到结束标记 cfloop 与 putback.cfm 中的开始 cfloop 一起出现,显然它不像我所理解的那样透明。
任何人都可以解释为什么这不起作用 and/or 提供解决方法。
I thought the include meant that "putback.cfm" would be picked up and dumped into that spot in the program.
关闭,但仅在运行时放置和处理包含。由于表达式错误(由 CFML 解析器),您没有达到那个点。
it is not recognizing that the end tag cfloop goes with with beginning cfloop in putback.cfm
解析器运行您的 .cfm 模板以在执行任何代码(运行时)之前构建 AST。根据解析器,循环永远不会关闭,因为 include 是另一个 .cfm 模板,它是单独检查的,并且从未在其包含状态中被触及。
修复
<!--- stuff which is different in each program --->
是应该包含的部分。但是,这种编码习惯被认为是不好的,最终会导致 Spaghetti code as you require, reuse and depend on variables inside of the loop. Your include should not be aware of its surroundings. If you absolutely have to do it this way, at least use cfmodule 将变量传递给您的 include。
解决这个问题的正确方法是使用接口。您指定所需的内容,提供您拥有的内容,然后让 <!--- stuff which is different in each program --->
为该界面提供服务。此代码结构将基于 cfcomponent and its cffunctions, implementing cfinterface.