Expressions
Simple csharp expressions
Any csharp syntax can appear as $expression; or ${expression}
The expression text itself is reproduced verbatim as a StringBuilder.Output(expression); line. So anything that evaluates to a string, simple type, or object with ToString? can be used in an expression.
<div>
<p>The time is ${DateTime.Now}.</p>
<p>The time is $DateTime.Now;.</p>
</div>
Using Mvc Helpers
There are instances of HtmlHelper, UrlHelper, and AjaxHelper available as Html, Url, and Ajax properties respectively. Any of their methods which return information can be used in csharp expressions.
<!-- Link to the Sort action on the current controller -->
<p>$Html.ActionLink("Click me", "Sort");</p>
<!-- Put out data in a way that's safe -->
<p>$Html.Encode(stuff);</p>
Conditional expression elements
The elements if, else, and elseif can be used to produce content conditionally. The if and elseif elements must have a condition attribute which is used literally as the boolean csharp expression.
<var x='5'/> <if condition='x == 5'> <p class='resultmessage'>Some value is five</p> </if>
An if element may be followed by any number of optional elseif element and finally a single, optional else element. Only whitespace may be between the if/elseif/else elements.
<viewdata user='UserInfo'/> <if condition='!user.IsLoggedIn()'> <p>Here's a login form</p> </if> <elseif condition='user.HasRole(RoleType.Administrator)'> <p>Hello - you're an admin</p> </elseif> <elseif condition='user.HasRole(RoleType.Registered)'> <p>Hello - you're a registered user</p> </elseif> <else> <p>I have no idea what type of person you are</p> </else>
Conditional expression attributes
The if and elseif attributes can be used on a normal element. This has the same effect as having a wrapping if or elseif element.
<var x='5'/> <p if='x==5' class='resultmessage'>Some value is five</p>
The same rules about order and whitespace apply.
<use namespace='SampleApp.Models'/> <viewdata user='UserInfo'/> <p if='!user.IsLoggedIn()'>Here's a login form</p> <p elseif='user.HasRole(RoleType.Administrator)'>Hello - you're an admin</p> <p elseif='user.HasRole(RoleType.Registered)'>Hello - you're a registered user</p> <else> <p>I have no idea what type of person you are</p> </else>
Looping and iteration
A foreach loop is produced with the element for and the attribute each. The each attribute is used verbatim so must have the type, variable name, "in", and collection.
<viewdata Posts="IList[[MyApp.Models.Post]"/> <for each="var post in Posts"> <p>$post.Title;</p> </for>
The for element can also have additional attributes which will evaluated as a variable assignment.
<var styles='new [] {"even", "odd"}' i="0">
<for each="var product in Products" i="i+1">
<p class="$styles[i%2];">$Html.Encode(product.Name);</p>
</for>
</var>
Iteration with the each attribute
You may also add the each attribute to any plain element.
<table>
<tr>
<td>Name</td>
<td>Type</td>
</tr>
<tr each='var user in users'>
<td>$user.Name;</td>
<td>$user.UserType;</td>
</tr>
</table>
If you do that other attributes are not treated as variable assignment, but they may contain $expression; evaluation.
<table>
<tr>
<td>Name</td>
<td>Type</td>
</tr>
<var classes='new [] {"even","odd"}' i="0">
<tr each='var user in users' class='$classes[++i%2];'>
<td>$i;) $user.Name;</td>
<td>$user.UserType;</td>
</tr>
</var>
</table>
Using namespace
You can add using statements to avoid typing out fully qualified type names, and to make extension methods to helpers available.
<use namespace="System.Collections.Generic"/> <use namespace="System.Web.Mvc"/> <viewdata Names="IList[[string]]"/>
The use namespace elements may appear anywhere in the view, master, or partial template files. The namespaces are de-duplicated so it's safe to include them where they're needed in views and partials if you prefer even though the same namespace could appear several times.
