Sure.
The complete nVelocity template:
<div>
Bet summary:
<ul>
#foreach( $bet in $bets )
<li>
$bet.Date $bet.Race
#if($bet.Amount > 10)
<strong>$bet.Amount.ToString("c")</strong>
#else
$bet.Amount.ToString("c")
#end
</li>
#end
</ul>
</div>
The Bet class:
public class Bet
{
public Bet(decimal amount, string race, DateTime date)
{
Amount = amount;
Race = race;
Date = date;
}
public decimal Amount { get; set; }
public string Race { get; set; }
public DateTime Date { get; set; }
}
Program:
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Commons.Collections;
using NVelocity.App;
using NVelocity;
using NVelocity.Runtime;
namespace nVelocityTest
{
public class Program
{
private static void Init()
{
var props = new ExtendedProperties();
props.AddProperty(RuntimeConstants_Fields.FILE_RESOURCE_LOADER_PATH, @"C:\dev\RnD\nVelocity\nVelocityTest\nVelocityTest\EmailTemplates");
Velocity.Init(props);
}
static void Main()
{
Init();
ICollection<Bet> bet = new Collection<Bet> { new Bet(10, "Banana Race", DateTime.Now), new Bet(15, "Potatoe Race", DateTime.Now) };
GenerateBetSummaryEmail(bet);
}
private static void GenerateBetSummaryEmail(ICollection<Bet> bets)
{
var context = new VelocityContext();
context.Put("bets", bets);
var writer = new System.IO.StringWriter();
try
{
Velocity.MergeTemplate("BetConfirmationEmailTemplate.vm", context, writer);
}
catch (Exception e)
{
Console.WriteLine("Problem merging template : " + e);
}
var outputTest = writer.GetStringBuilder().ToString();
}
}
}
Expected outputTest:
Below is your bet summary:
- 25/03/2010 9:21:15 AM
Banana Race $10.00
-
25/03/2010 9:21:15 AM Potatoe Race
$15.00
Actual outputTest:
Below is your bet summary:
- 25/03/2010 9:21:15 AM
Banana Race $10.00
-
25/03/2010 9:21:15 AM Potatoe Race
$15.00
As previously mentioned, the comparison #if($bet.Amount > 10) in the nVelocity template fails even though, in the second bet object, the value of bet.Amount is 15. If Amount is changed to be of type int, the comparison works as expected.