Home

Castle Stronghold

Table of contents

NVelocity Improvements

Here is a list of improvements made to the original NVelocity code. If you download the code from our code repository make sure you check the Changes.txt as we log all changes on it.

ParamArray methods

Methods that use the ParamArray attribute (in C# the params keyword defines the attribute) allows the compilers (participants) to define a better syntax for invocation. In fact the compiler is in charge of creating the array on the programmers behalf.

NVelocity is able to make such invocations also creating the correct array for you. For example:


public static string SomeMethod(params String[] args)
{
    return String.Join('-', args);
}

$instance.SomeMethod('arg1', 'arg2')    

Which will output

arg1-arg2

The arguments are evaluated normally, so you are free to use references or string interpolation as you wish.

Built-in support for dictionaries

Dictionaries, if supported natively by a compiler, can make the code more self documented than anything else. Due to the usage on MonoRail, dictionary support was introduced to NVelocity.

All you have to to is construct a string using a special content. It will be matched using a regular expression and the dictionary will be constructed, populated and passed as a parameter for the method instead of the string. For example:


$HtmlHelper.LabelFor('elementid', 'Name:', "%{class='required', accessKey='N'}")

The code above will construct a dictionary and add the entries class and accessKey.

Fancy foreach loops

Inspired on FogCreek's CityDesk language, the following type of foreach is supported.


#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

All inner sections are optional, and they can appear in any order multiple times (sections with same name will have their content appended)

So for example you can use it to create a table contents with alternating styles:


#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

Which will output something like:


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

If the $people variable was null the output would be:


Sorry No Person Found

Binary expressions with enum types

In the old version of NVelocity, enums could only be compared if we put the elements on the template context. That was very odd to say the least.

To solve the problem and at the same time make the code readable, binary expressions were changed to compare enums with their textual representation. For example:


public enum OrderStatus
{
  Undefined,
  Created,
  Dispatched
}

You can use it in the following way


#if($order.Status == "Undefined")
  Sorry, but we don't know this order.
#elseif($order.Status == "Created")
  Your order is being processed. Hold on!
#elseif($order.Status == "Dispatched")
  Your order has been dispatched through UPS. Cross your fingers!
#end

Unreleased features

These should be available on Castle Project 1.0 RC 3.

Case-insensitive properties and methods

Method calls and properties are no longer case-sensitive. That means you no longer need to remember the case of your method and properties while writing your NVelocity templates.

Dictionary support improved

Now it supports string interpolation anywhere and integers and singles:

UsageBehavior
key='value'value is treated as String
key=1value is converted to int
key=1.2value is converted to single
key='1'value is treated as String
$key='value'$key will be evaluated (cannot be evaluated to null)
key=$valuevalue is evaluated and the object is added (not converted to string)
key='some$value'value is evaluated and concatenated with the literal value

Google
Search WWW Search castleproject.org