Coldfusion-回顾过去5年并插入select
我试图把从2011年开始的过去5年变成一个或选择标签,并将其作为选项输出。这是我目前的解决方案Coldfusion-回顾过去5年并插入select,select,coldfusion,Select,Coldfusion,我试图把从2011年开始的过去5年变成一个或选择标签,并将其作为选项输出。这是我目前的解决方案 <cfset dtStart = #year(now())# /> <cfset dtToday = dtStart /> <select name="date"> <cfloop index="intDayOffset" from="0" to="4" step="1"> <cfset date = (#dtToday#
<cfset dtStart = #year(now())# />
<cfset dtToday = dtStart />
<select name="date">
<cfloop index="intDayOffset" from="0" to="4" step="1">
<cfset date = (#dtToday#-#intDayOffset#) />
<cfoutput><option value="#date#">#date#</option></cfoutput>
</cfloop>
</select>
有谁能想出一个更优雅的方法来实现这一点吗?既然您将CF与PHP进行比较,认为可读性和简洁性很重要,我想我会抛弃您的解决方案在Ruby+中的样子:
<cfloop from="#year(now())#" to="#year(now())-5#" index="y" step="-1">
#y#
</cfloop>
请随意投票否决这个答案,因为它不能直接回答问题。我不是想让你相信CF不好或Haml更好,只是给你展示了一个替代方案。这稍微简化了一点
<cfloop index="date" from="#dtToday#" to="#dtToday - 5#" step="-1">
<cfoutput><option value="#date#">#date#</option></cfoutput>
</cfloop>
我会这样处理的
<select name="date">
<cfloop index="intDayOffset" from="0" to="4" step="1">
<cfoutput>
<option value="#year(dateAdd('yyyy',-intDayOffset, now()))#">#year(dateAdd('yyyy',-intDayOffset, now()))#</option>
</cfoutput>
</cfloop>
</select>
看到其他语言如何处理事情总是很有趣的。
<select name="date">
<cfloop index="intDayOffset" from="0" to="4" step="1">
<cfoutput>
<option value="#year(dateAdd('yyyy',-intDayOffset, now()))#">#year(dateAdd('yyyy',-intDayOffset, now()))#</option>
</cfoutput>
</cfloop>
</select>