NVelocity练习
代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Commons.Collections;
using NVelocity;
using NVelocity.App;
using NVelocity.Context;
using NVelocity.Runtime;
namespace TestConsoleAppCastleNVelocity
{
class Program
{
static void Main(string[] args)
{
//Step 1 - Creating a VelocityEngine
VelocityEngine engine = new VelocityEngine();
ExtendedProperties props = new ExtendedProperties();
props.AddProperty(RuntimeConstants.RESOURCE_LOADER, "file");
props.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH,
"E:/_Projects/DotNet/TestConsoleAppCastleNVelocity/TestConsoleAppCastleNVelocity/templates/");
props.AddProperty(RuntimeConstants.INPUT_ENCODING, "utf-8");
props.AddProperty(RuntimeConstants.OUTPUT_ENCODING, "utf-8");
engine.Init(props);
//Step 2 - Creating the Template
Template template = engine.GetTemplate("First.vm");
//Step 3 - Merging the template
VelocityContext context = new VelocityContext();
context.Put("from", "somewhere");
context.Put("to", "someone");
context.Put("subject", "Welcome to NVelocity");
context.Put("customer", new Customer("顾客姓名吴XX"));
List<Customer> list = new List<Customer>();
list.Add(new Customer("张三"));
list.Add(new Customer("李四"));
list.Add(new Customer("五五"));
list.Add(new Customer("赵六"));
list.Add(new Customer("李七"));
context.Put("customerList", list);
StringWriter writer = new StringWriter();
template.Merge(context, writer);
Console.WriteLine( writer.GetStringBuilder().ToString() );
//Console.WriteLine( System.AppDomain.CurrentDomain.BaseDirectory );
Console.ReadKey();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Commons.Collections;
using NVelocity;
using NVelocity.App;
using NVelocity.Context;
using NVelocity.Runtime;
namespace TestConsoleAppCastleNVelocity
{
class Program
{
static void Main(string[] args)
{
//Step 1 - Creating a VelocityEngine
VelocityEngine engine = new VelocityEngine();
ExtendedProperties props = new ExtendedProperties();
props.AddProperty(RuntimeConstants.RESOURCE_LOADER, "file");
props.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH,
"E:/_Projects/DotNet/TestConsoleAppCastleNVelocity/TestConsoleAppCastleNVelocity/templates/");
props.AddProperty(RuntimeConstants.INPUT_ENCODING, "utf-8");
props.AddProperty(RuntimeConstants.OUTPUT_ENCODING, "utf-8");
engine.Init(props);
//Step 2 - Creating the Template
Template template = engine.GetTemplate("First.vm");
//Step 3 - Merging the template
VelocityContext context = new VelocityContext();
context.Put("from", "somewhere");
context.Put("to", "someone");
context.Put("subject", "Welcome to NVelocity");
context.Put("customer", new Customer("顾客姓名吴XX"));
List<Customer> list = new List<Customer>();
list.Add(new Customer("张三"));
list.Add(new Customer("李四"));
list.Add(new Customer("五五"));
list.Add(new Customer("赵六"));
list.Add(new Customer("李七"));
context.Put("customerList", list);
StringWriter writer = new StringWriter();
template.Merge(context, writer);
Console.WriteLine( writer.GetStringBuilder().ToString() );
//Console.WriteLine( System.AppDomain.CurrentDomain.BaseDirectory );
Console.ReadKey();
}
}
}
模板代码:
代码
From来自: $from
To去: $to
Subject主题: $subject
Hello $customer.Name
We're please to yada yada yada.
#set($myInt=1024)
${myInt}s
****************************
#set ( $bFlag=3 )
#if($bFlag==1)
It's one
#elseif($bFlag==2)
It's two
#else
It's three
#end
##这是注释
#*
这是多行
注释
*#
*******foreach test********
<table>
#foreach( $customer in $customerList )
<tr><td>$velocityCount
#if($velocityCount%2==0) 偶数行 #else 奇数行 #end
</td><td>$customer.Name</td></tr>
#end
</table>
To去: $to
Subject主题: $subject
Hello $customer.Name
We're please to yada yada yada.
#set($myInt=1024)
${myInt}s
****************************
#set ( $bFlag=3 )
#if($bFlag==1)
It's one
#elseif($bFlag==2)
It's two
#else
It's three
#end
##这是注释
#*
这是多行
注释
*#
*******foreach test********
<table>
#foreach( $customer in $customerList )
<tr><td>$velocityCount
#if($velocityCount%2==0) 偶数行 #else 奇数行 #end
</td><td>$customer.Name</td></tr>
#end
</table>