之前我转载过一篇:《Velocity 模板使用指南》中文版[转] ,这个是基于最开始的Java版下的Velocity来说的。后来在castle的.NET版下又提供了一些增强功能,详见:NVelocity Improvements
本文结合一些小例子来具体谈谈这些增强功能的使用。
(有部分内容是直接翻译自NVelocity Improvements -_-)
一、支持数组参数
比如在Controller中定义如下方法:
arg1-arg2
二、内置字典支持
对于一些传参的地方很方便,比如我们常用的一种方式:
内置字典我们可以在很多场合用到,比如我们在Controller中定义一个方法:
然后在vm中调用:
三、更强的foreach功能(这个功能比较好)
可以指定在foreach之前、之后等特定时候执行一些语句,具体语法如下:
四、枚举类型的改进
为了可读性,可以自己使用枚举类型的文字表达进行比较。
例:
Castle1.0 RC3中的新功能:
1、在vm中,方法和属性不再区分大小写,使用时可以不必记住大小写了
2、字典功能改进,在vm字典调用时可以直接使用以下方式(参见上面的内置字典支持):
key='value' key=1 key=1.2 key='1' $key='value' key=$value key='some$value'
本文结合一些小例子来具体谈谈这些增强功能的使用。
(有部分内容是直接翻译自NVelocity Improvements -_-)
一、支持数组参数
比如在Controller中定义如下方法:
public void Index()
{
PropertyBag.Add("instance", this);
}
public static string Welcome(params String[] args)
{
return String.Join("-", args);
}
在vm中写:{
PropertyBag.Add("instance", this);
}
public static string Welcome(params String[] args)
{
return String.Join("-", args);
}
$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();
}
{
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
比如如下的一个例子:#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:#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
<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时会直接输出:<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>
Sorry No Person Found
四、枚举类型的改进
为了可读性,可以自己使用枚举类型的文字表达进行比较。
例:
public enum OrderStatus
{
Undefined,
Created,
Dispatched
}
那么可以在vm中如下比较:{
Undefined,
Created,
Dispatched
}
#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
(原文中好像有点问题,我重新改了一些代码)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,如需转载请自行联系原作者