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

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

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

        
public   static   string  Welcome( params  String[] args)
        
{
            
return String.Join("-", args);
        }
在vm中写:
$instance.Welcome( ' arg1 ' ' arg2 ' )
那么回输出如下结果:
arg1-arg2

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

内置字典我们可以在很多场合用到,比如我们在Controller中定义一个方法:
         public   string  DictionaryTest( string  name, IDictionary attributes)
        
{
            StringBuilder sResult 
= new StringBuilder("<input type=\"text\" name='" + name + "'");
            
foreach (object key in attributes.Keys)
            
{
                
object value = attributes[key];
                sResult.Append(
" " + key + "='" + value + "");
            }


            sResult.Append(
"/>");

            
return sResult.ToString();
        }

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

三、更强的foreach功能(这个功能比较好)
可以指定在foreach之前、之后等特定时候执行一些语句,具体语法如下:
#foreach($i in $items)
#each (this is optional since its the default section)
       text which appears for each item
#before
       text which appears before each item
#after
       text which appears after each item
#between
       text which appears between each two items
#odd
       text which appears for every other item, including the first
#even
       text which appears for every other item, starting with the second
#nodata
       Content rendered if $items evaluated to null or empty
#beforeall
       text which appears before the loop, only if there are items
       matching condition
#afterall
       text which appears after the loop, only of there are items
       matching condition
#end
比如如下的一个例子:
#foreach($person in $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 >< td  colspan ='2' > $person.bio </ td ></ tr >

#afterall
       
</ table >

#nodata
       Sorry No Person Found
#end
当我们$people中有两条记录时会生成以下html:
< table >
       
< tr >
       
< th > Name </ th >
       
< th > Age </ th >
       
</ tr >
       
< tr  style ='color:white' >
               
< td > John </ td >
               
< td > 32 </ td >
       
</ tr >
       
< tr >< td  colspan ='2' > Monorail programmer </ td ></ tr >
       
< tr  style ='color:gray' >
               
< td > Jin </ td >
               
< td > 12 </ td >
       
</ tr >
       
< tr >< td  colspan ='2' > Castle guru </ td ></ tr >
</ table >
当$people为null时会直接输出:
Sorry No Person Found

四、枚举类型的改进
为了可读性,可以自己使用枚举类型的文字表达进行比较。
例:
public   enum  OrderStatus
{
  Undefined,
  Created,
  Dispatched
}
那么可以在vm中如下比较:
#if($order == "Undefined")
  Sorry, but we don't know this order.
#elseif($order == "Created")
  Your order is being processed. Hold on!
#elseif($order == "Dispatched")
  Your order has been dispatched through UPS. Cross your fingers!
#end
( 原文中好像有点问题,我重新改了一些代码)

Castle1.0 RC3中的新功能:
1、在vm中,方法和属性不再区分大小写,使用时可以不必记住大小写了
2、字典功能改进,在vm字典调用时可以直接使用以下方式(参见上面的 内置字典支持):
    key='value' key=1 key=1.2 key='1' $key='value' key=$value key='some$value'
    本文转自永春博客园博客,原文链接:http://www.cnblogs.com/firstyi/archive/2007/11/02/945982.html ,如需转载请自行联系原作者
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值