`
raisun_1988
  • 浏览: 113822 次
  • 性别: Icon_minigender_1
  • 来自: 长沙
社区版块
存档分类
最新评论

MonoRail学习笔记十三:NVelocity的增强功能

    博客分类:
  • .Net
阅读更多

之前我转载过一篇:《Velocity 模板使用指南》中文版[转] ,这个是基于最开始的Java版下的Velocity来说的。后来在castle的.NET版下又提供了一些增强功能,详见:NVelocity Improvements
本文结合一些小例子来具体谈谈这些增强功能的使用。
(有部分内容是直接翻译自NVelocity Improvements-_-)

一、支持数组参数
比如在Controller中定义如下方法:
publicvoidIndex()
{
PropertyBag.Add(
"instance",this);
}

publicstaticstringWelcome(paramsString[]args)
{
returnString.Join("-",args);
}
在vm中写:
$instance.Welcome('arg1','arg2')
那么回输出如下结果:
arg1-arg2

二、内置字典支持
对于一些传参的地方很方便,比如我们常用的一种方式:
$HtmlHelper.LabelFor('elementid','Name:',"%{class='required',accessKey='N'}")
那么会自动生成一个字典,里面包含class和accessKey两个条目

内置字典我们可以在很多场合用到,比如我们在Controller中定义一个方法:
publicstringDictionaryTest(stringname,IDictionaryattributes)
{
StringBuildersResult
=newStringBuilder("<inputtype=\"text\"name='"+name+"'");
foreach(objectkeyinattributes.Keys)
{
objectvalue=attributes[key];
sResult.Append(
""+key+"='"+value+"'");
}


sResult.Append(
"/>");

returnsResult.ToString();
}

然后在vm中调用:
$instance.DictionaryTest('id',"%{aa='aa1',value='aa2',value2='aa3'}")
会在页面中生成一个输入框,具体的html代码是:
<inputtype="text"name='id'aa='aa1'value='aa2'value2='aa3'/>

三、更强的foreach功能(这个功能比较好)
可以指定在foreach之前、之后等特定时候执行一些语句,具体语法如下:
#foreach($iin$items)
#each(thisisoptionalsinceitsthedefaultsection)
textwhichappearsforeachitem
#before
textwhichappearsbeforeeachitem
#after
textwhichappearsaftereachitem
#between
textwhichappearsbetweeneachtwoitems
#odd
textwhichappearsforeveryotheritem,includingthefirst
#even
textwhichappearsforeveryotheritem,startingwiththesecond
#nodata
Contentrenderedif$itemsevaluatedtonullorempty
#beforeall
textwhichappearsbeforetheloop,onlyifthereareitems
matchingcondition
#afterall
textwhichappearsaftertheloop,onlyofthereareitems
matchingcondition
#end
比如如下的一个例子:
#foreach($personin$people)
#beforeall
<table>
<tr>
<th>Name</th>
<th>Age</th>
</tr>
#before
<tr
#odd
Style
='color:gray'>
#even
Style='color:white'>

#each
<td>$person.Name</td>
<td>$person.Age</td>

#after
</tr>

#between
<tr><tdcolspan='2'>$person.bio</td></tr>

#afterall
</table>

#nodata
SorryNoPersonFound
#end
当我们$people中有两条记录时会生成以下html:
<table>
<tr>
<th>Name</th>
<th>Age</th>
</tr>
<trstyle='color:white'>
<td>John</td>
<td>32</td>
</tr>
<tr><tdcolspan='2'>Monorailprogrammer</td></tr>
<trstyle='color:gray'>
<td>Jin</td>
<td>12</td>
</tr>
<tr><tdcolspan='2'>Castleguru</td></tr>
</table>
当$people为null时会直接输出:
SorryNoPersonFound

四、枚举类型的改进
为了可读性,可以自己使用枚举类型的文字表达进行比较。
例:
publicenumOrderStatus
{
Undefined,
Created,
Dispatched
}
那么可以在vm中如下比较:
#if($order=="Undefined")
Sorry,butwedon'tknowthisorder.
#elseif($order=="Created")
Yourorderisbeingprocessed.Holdon!
#elseif($order=="Dispatched")
YourorderhasbeendispatchedthroughUPS.Crossyourfingers!
#end
(原文中好像有点问题,我重新改了一些代码)

Castle1.0 RC3中的新功能:
1、在vm中,方法和属性不再区分大小写,使用时可以不必记住大小写了
2、字典功能改进,在vm字典调用时可以直接使用以下方式(参见上面的内置字典支持):
key='value' key=1 key=1.2 key='1' $key='value' key=$value key='some$value'
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics