當然。
完整nVelocity模板:
<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>
打賭類:
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; }
}
計劃:
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();
}
}
}
預計outputTest:
下面是你的賭注摘要:
- 25/03/2010上午09時21分15秒 香蕉賽$ 10.00
- 25/03/2010上午09時21分15秒土豆賽 $ 15.00
實際outputTest:
下面是你的賭注摘要:
- 25/03/2010上午9點21分15秒 香蕉賽$ 10.00
- 25/03/2010上午9點21分15秒土豆賽 $ 15.00
如前所述,即使在第二個賭注對象中,bet.Amount的值爲15,nVelocity模板中的比較#if($ bet.Amount> 10)也會失敗。如果金額更改爲鍵入int,比較按預期工作。
您是否使用最新的NVelocity版本? (截至撰寫本文時爲止) – 2010-03-25 12:55:24
就是這樣! 非常感謝您的幫助。 如何將您的評論標記爲答案? – Bart 2010-03-25 23:04:12