views:

115

answers:

2

I have a simple object that has as one of it's properties a decimal named Amount. When I attempt a comparison on this property as part of an nVelocity template, the comparison always fails. If I change the property to be of type int the comparison works fine. Is there anything extra I need to add to the template for the comparison to work?

Below is a sample from the aforementioned template:

#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

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

Any help would be greatly appreciated.

+2  A: 

I tested this and it worked. It seems this was a bug that isn't present any more in the latest release of NVelocity (1.1 as of this writing).

Mauricio Scheffer
A: 

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.

Bart
are you using the latest NVelocity release? (1.1 as of this writing)
Mauricio Scheffer
That was it!!Thanks so much for your help.How do I mark your comment as the answer?
Bart