Variables available in NVelocity templates
NVelocityViewEngine Variables
The NVelocityViewEngine is responsible for making "useful" variables available to your view. Here's the list of variables added to the context by the NVelocityViewEngine:
| Context Variable | Description |
|---|---|
| $controller | The controller being executed. |
| $context | The IRailsEngineContext. |
| $request | context.Request |
| $response | context.Response |
| $session | context.Session |
| $childContent | Used inside Layouts. It defines the content rendered by a View. |
| $page | Available in *.njs views and is added in the GenerateJS method. |
| $siteroot | context.ApplicationPath |
Additionally - the contents of the following collections are merged into the context.
- controller.Resources
- context.Params
- controller.Helpers
- context.Flash
- controller.PropertyBag
Each key in each of the collections becomes a $variable. For example:
class MyController {
public void Index() {
PropertyBag["myvariable"] = "some value";
Context.Params["othervariable"] = "some other value value";
Context.Flash["anothervariable"] = "yet one more";
}
}
In your view you will have the following variables:
$myvariable
$othervariable
$anothervariable
Helpers are also added to allow you to invoke static members on some common types:
- $Byte
- $SByte
- $Int16
- $Int32
- $Int64
- $UInt16
- $UInt32
- $UInt64
- $Single
- $Double
- $Boolean
- $Char
- $Decimal
- $String
- $Guid
- $DateTime
This allows you to do useful things like:
The Current time is: $DateTime.Now
Displaying the contents of Dictionary items.
When handling Dictionary items (such as a Hashtable or a generic Dictionary
public class MyController {
public void Index() {
PropertyBag["rightnow"] = DateTime.Now.ToLongTimeString();
PropertyBag["result"] = 5 / 3;
PropertyBag["PropertyBag"] = PropertyBag;
}
}
then in your view you can iterate over the $PropertyBag:
<h1>Property bag variables</h1>
#foreach($de in $PropertyBag)
#beforeall
<table border="1">
<tr>
<th>Name</th>
<th>value</th>
</tr>
#each
<tr>
<td>$de.Key</td>
<td>$de.Value</td>
</tr>
#afterall
</table>
#end