확실히.
전체 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 오전 9시 21분 15초 바나나 인종 $ 10.00
- 25/03/2010 오전 9시 21분 15초의 Potatoe 인종 $ 15.00
: 다음은 당신의 내기 요약
실제 outputTest :
다음은 당신의 내기 요약 한 것입니다
- 25/03/2010 오전 9시 21분 15초 바나나 인종 $ 10.00
- 25/03/2010 오전 9시 21분 15초의 Potatoe 인종 $ 15.00
앞서 언급했듯이 nVelocity 템플릿의 비교 #if ($ bet.Amount> 10)는 두 번째 bet 개체에서 bet.Amount의 값이 15인데도 실패합니다. 형식 int, 비교 예상대로 작동합니다.
최신 NVelocity 버전을 사용하고 계십니까? (이 글을 쓰는 시점에서 1.1) –
그게 다야 !! 도움을 주셔서 대단히 감사드립니다. 댓글을 답글로 어떻게 표시합니까? – Bart