Home

Castle Stronghold

Table of contents

  • 1 NVelocity files
  • 2 Layouts
  • 3 Configuring NVelocity
  • 4 Macros
  • 5 WebForms vs NVelocity
  • 6 Fancy Loops

NVelocity View Engine

The NVelocity View Engine uses NVelocity

Frequently Asked Questions on NVelocity View Engine can be found on the standard MonoRail FAQ.

To use NVelocity View Engine inform the type on the customEngine on the configuration file:


<viewEngine 
    viewPathRoot="views" 
    customEngine="Castle.MonoRail.Framework.Views.NVelocity.NVelocityViewEngine, Castle.MonoRail.Framework.Views.NVelocity" />

NVelocity files

NVelocity uses the extension .vm so just create your views with that extension. Remember that from your controller you should not reference file extensions when defining views to render.

Layouts

Use $childContent context variable to render the content of the view on the layout template. The following is a simple layout using NVelocity:


<html>

Welcome

$childContent

Footer

</html>
    
Quick Note

The view template selected by the controller is executed before the layout template. In fact the layout template is merged with the result of the view template execution.

Configuring NVelocity

The NVelocity View Engine looks for a file nvelocity.properties in the root of the view folder. You can use this file to configure how NVelocity should behave.

For example, to configure NVelocity to support Chinese encoding create a text file named nvelocity.properties, save it to your views folder and add the following content:

input.encoding=GB2312
output.encoding=GB2312

More information on the entries can be found on the original Velocity documentation.

Macros

The NVelocity supports macros, but keep in mind that they have problems. If you want to use macros you can create a folder macros under the your views root folder.

All .vm files in this folder will be loaded as a NVelocity Macro library so the macros will be available to all templates.

WebForms vs NVelocity

There is a common FUD in moving from WebForms to NVelocity. NVelocity promotes a better separation of concerns than WebForms (the issues with WebForms are stated in the Asp.Net View Engine document).

So try NVelocity View Engine with a small project that demands common tasks like pagination, authentication and draw your own conclusions.

Fancy Loops

Inspired on http://www.fogcreek.com/CityDesk/2.0/help/Scripting_With_CityScript/FancyLoops.html. The following code should be self-explanatory:


#foreach($i in $items)
#each (this is optional since it's 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 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 will be:


Sorry No Person Found
Google
Search WWW Search castleproject.org