2010-03-24 3 views
1

속성 중 하나에 Amount라는 십진수가있는 단순한 개체가 있습니다. nVelocity 템플릿의 일부로이 속성에 대한 비교를 시도하면 비교가 항상 실패합니다. 만약 int 형으로 프로퍼티를 변경하면 비교가 잘됩니다. 비교를 위해 템플릿에 추가해야 할 것이 있습니까?nVelocity - 십진 속성에서 '보다 큼'비교를 시도 할 때 템플릿 문제가 발생했습니다.

다음
#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 

는 베팅 클래스입니다 : 어떤 도움을 크게 감상 할 수

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

아래

은 상기 템플릿 샘플입니다.

답변

2

I tested this and it worked. 이것은 NVelocity의 최신 릴리스에 더 이상 존재하지 않는 버그였습니다 (이 글을 쓰는 시점에서 1.1).

0

확실히.

전체 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, 비교 예상대로 작동합니다.

+0

최신 NVelocity 버전을 사용하고 계십니까? (이 글을 쓰는 시점에서 1.1) –

+0

그게 다야 !! 도움을 주셔서 대단히 감사드립니다. 댓글을 답글로 어떻게 표시합니까? – Bart