-
Notifications
You must be signed in to change notification settings - Fork 0
/
CommonHelper.cs
29 lines (26 loc) · 1.03 KB
/
CommonHelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using NVelocity.App;
using NVelocity.Runtime;
using NVelocity;
namespace LYB
{
public class CommonHelper
{
public static string RenderHtml(string name, object data)
{
VelocityEngine vltEngine = new VelocityEngine();
vltEngine.SetProperty(RuntimeConstants.RESOURCE_LOADER, "file");
vltEngine.SetProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, System.Web.Hosting.HostingEnvironment.MapPath("~/template"));//模板文件所在的文件夹
vltEngine.Init();
VelocityContext vltContext = new VelocityContext();
vltContext.Put("Data", data);//设置参数,在模板中可以通过$data来引用
Template vltTemplate = vltEngine.GetTemplate(name);
System.IO.StringWriter vltWriter = new System.IO.StringWriter();
vltTemplate.Merge(vltContext, vltWriter);
return vltWriter.GetStringBuilder().ToString();
}
}
}