CastleNVelocity 使用demo

  templete.vm

你好

尊敬的$people.Name,欢迎使用$CompanyName公司的$GoodsName产品。

                      发送时间:$SendTime

$foo   

$bar

#if( $foo == "6")

it's true!

#else

it's not!

#end

#*

这是多行注视

*#

方法的调用

$people.dowork();

调用foreach循环

#foreach($people in $peopleList)

$people.name

#end

 1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Web;
5 using System.Web.UI;
6 using System.Web.UI.WebControls;
7
8 using Commons.Collections;
9 using NVelocity; //第三方DLL库
10 using NVelocity.App;
11 using NVelocity.Context;
12 using System.IO;
13 using System.Collections;
14 namespace WebApplication2
15 {
16 public partial class WebForm1 : System.Web.UI.Page
17 {
18 protected void Page_Load(object sender, EventArgs e)
19 {
20 this.TextBox1.Text = ChangeTempelete();
21 }
22
23 private string ChangeTempelete()
24 {
25 IList<PeopleModel> pList = new List<PeopleModel>();
26 for (int i = 0; i < 20; i++)
27 {
28 PeopleModel p = new PeopleModel("名字" + i.ToString(), i);
29 pList.Add(p);
30 p = null;
31 }
32 PeopleModel pp = new PeopleModel("haha", 25);
33 VelocityEngine velocity = new VelocityEngine(); //创建模版替换引擎
34 ExtendedProperties props = new ExtendedProperties(); //创建模版使用的扩展属性
35
36 props.SetProperty("file.resource.loader.path", new ArrayList(new string[] { ".", "D:\\lianxi\\" })); //模板文件的查找地址
37 velocity.Init(props); //向模板中导入前面创建的属性
38      
//模板文件的文件 与上面为完整的路劲,NVeloctiy回去这个完整路径下搜寻响应的模版文件。否则,会报错找哦不到资源!

39 Template template = velocity.GetTemplate("templete.vm");
40
41 VelocityContext context = new VelocityContext();
42 context.Put("people", pp);
43 context.Put("CompanyName", "microsoft");
44 context.Put("GoodsName", "window .net framework");
45 context.Put("SendTime", DateTime.Now);
46 context.Put("foo", "6");
47 context.Put("bar", "5");
48 context.Put("people", pp);
49 context.Put("peopleList", pList);
50
51 StringWriter writer = new StringWriter();
52 template.Merge(context, writer);
53 return writer.GetStringBuilder().ToString();
54 }
55 }
56 }

  更多详细内容访问

 1.包含模版中的语法

  

     http://velocity.apache.org/engine/releases/velocity-1.5/vtl-reference-guide.html

  2.包含demo

  http://www.castleproject.org/others/nvelocity/usingit.html 含例子

项目下载

  https://files.cnblogs.com/dongchunfeng/CastleNVelocity-1.1.1.rar

  

posted @ 2011-08-16 10:39  来了就看看  阅读(363)  评论(0编辑  收藏  举报