NVelocity 宏参数未计算
我正在寻找在我的 NVelocity 模板中创建一个内联函数(方法)。解决方案似乎是使用 Velocimacros。因此,我模拟了以下模板进行测试:
#macro( getOutput $one $two $three )
<td>$one</td>
<td>$two.Item2</td>
<td>$three</td>
#end
<table>
#foreach( $item in $mdl.Items )
<tr>
#set( $one1 = $item.Item1 )
#getOutput( $one1 $item $item.Item3 ) ## item.Item3 won't evaluate!
</tr>
#end
</table>
$mdl
是我的基本模型对象,在本示例中它包含一个属性 Items
,它是一个 List (元组(字符串、整数、日期))
。填充如下测试数据:
Dim items As New List(Of Tuple(Of String, Integer, DateTime))
With items
.Add(New Tuple(Of String, Integer, DateTime)("One", 1, #1/1/2001#))
.Add(New Tuple(Of String, Integer, DateTime)("Two", 2, #2/2/2002#))
.Add(New Tuple(Of String, Integer, DateTime)("Three", 3, #3/3/2003#))
End With
当我运行模板时,我遇到的问题是宏参数 $two
的输出实际上是“$item.Item3”,而不是评估为 #3 /3/2003#。 (顺便说一句 - 如果元组中的 3 个项目是通过 .Item
调用传递的,就会发生这种情况,所以它与数据类型无关)。
我可以创建一个变量并传递它($one1)。我可以传递元组本身并调用宏内的 .Item 属性 ($item.Item2),但由于某种原因,我在将参数传递给宏时无法调用 .Item
属性。有什么见解吗?
I'm looking to create an inline function (method) inside my NVelocity template. The solution to this appears to be to use Velocimacros. So, I mocked up the following template to test:
#macro( getOutput $one $two $three )
<td>$one</td>
<td>$two.Item2</td>
<td>$three</td>
#end
<table>
#foreach( $item in $mdl.Items )
<tr>
#set( $one1 = $item.Item1 )
#getOutput( $one1 $item $item.Item3 ) ## item.Item3 won't evaluate!
</tr>
#end
</table>
$mdl
is my base Model object, which for this example contains one property, Items
, which is a List(Of Tuple(Of String, Integer, Date))
. Populated with test data like so:
Dim items As New List(Of Tuple(Of String, Integer, DateTime))
With items
.Add(New Tuple(Of String, Integer, DateTime)("One", 1, #1/1/2001#))
.Add(New Tuple(Of String, Integer, DateTime)("Two", 2, #2/2/2002#))
.Add(New Tuple(Of String, Integer, DateTime)("Three", 3, #3/3/2003#))
End With
When I run the template, the problem I'm having is that the output from the macro parameter $three
is literally "$item.Item3" instead of evaluating to #3/3/2003#. (BTW - this happens with any of the 3 items in the tuple if they are passed with the .Item
call, so it isn't about the data type).
I can make a variable and pass it just fine ($one1). I can pass the tuple itself and call the .Item property inside the macro ($item.Item2), but for some reason I cannot call the .Item
property when passing the argument to the macro. Any insight?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看来 NVelocity 宏支持是有限的。
http://www.castleproject.org/others/nvelocity/problems.html#宏
另一种方法是使用助手:
NVelocity 扩展方法 ASP.NET Webform
It looks like NVelocity macro support is limited.
http://www.castleproject.org/others/nvelocity/problems.html#macros
An alternative would be to use Helpers:
NVelocity extension method ASP.NET webform