Home

Castle Stronghold

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 VariableDescription
$controllerThe controller being executed.
$contextThe IRailsEngineContext.
$request context.Request
$responsecontext.Response
$sessioncontext.Session
$childContentUsed inside Layouts. It defines the content rendered by a View.
$pageAvailable in *.njs views and is added in the GenerateJS method.
$siterootcontext.ApplicationPath

Additionally - the contents of the following collections are merged into the context.

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:

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), remember that the item returned from the enumerator is a DictionaryEntry structure (or it's generic equivalent) that exposes a "Key" and "Value" property. Here's an example showing how you can list the contents of the PropertyBag (a HybridDictionary)


    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
    

Google
Search WWW Search castleproject.org