NVelocity的增强功能

一、支持数组参数
比如在Controller中定义如下方法:
        public void Index()
        {
            PropertyBag.Add("instance", this);
        }
        public static string Welcome(params String[] args)
        {
            return String.Join("-", args);
        }在vm中写:
$instance.SomeMethod('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'

posted @ 2010-04-02 15:50  YaSin  阅读(654)  评论(0编辑  收藏  举报