本文結合一些小例子來具體談談這些增強功能的使用。
一、支援數組參數
比如在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


#end
比如如下的一個例子:

#foreach($person in $people)


<table>

<tr>

<th>Name</th>

<th>Age</th>

</tr>


<tr


Style='color:gray'>


Style='color:white'>


#each

<td>$person.Name</td>

<td>$person.Age</td>



</tr>



<tr><td colspan='2'>$person.bio</td></tr>



</table>



Sorry No Person Found


當我們$people中有兩條記錄時會生成以下html:

<table>

<tr>

<th>Name</th>

<th>Age</th>


<tr style='color:white'>

<td>John</td>

<td>32</td>


<tr><td colspan='2'>Monorail programmer</td></tr>

<tr style='color:gray'>

<td>Jin</td>

<td>12</td>


<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!

(原文中好像有點問題,我重新改了一些代碼)
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,如需轉載請自行聯系原作者