-
Notifications
You must be signed in to change notification settings - Fork 0
/
VelocityHelper.cs
64 lines (60 loc) · 1.92 KB
/
VelocityHelper.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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
using System;
using System.Collections.Generic;
using System.IO;
using NVelocity;
using NVelocity.App;
using log4net;
namespace Common.Engine
{
/// <summary>
/// 模板引擎工具类
/// </summary>
public static class VelocityHelper
{
//创建日志记录组件实例
private static readonly ILog Log = LogManager.GetLogger("fileLogger");
private static readonly VelocityContext BaseContext = new VelocityContext();
/// <summary>
/// 静态构造函数
/// </summary>
static VelocityHelper()
{
//nvelocity引擎初始化
Velocity.Init();
//模板上下文初始化,全局变量
BaseContext.Put("tmpDouble", 0.0);
}
/// <summary>
/// 渲染模板
/// </summary>
/// <param name="templ">模板字符串,如"$!a.getName"</param>
/// <param name="obj">模板传入参数,模板可直接调用,如$!param1</param>
/// <returns></returns>
public static string RenderString<T>(string templ, IDictionary<string, T> obj)
{
//模板为空,直接返回
if (string.IsNullOrEmpty(templ))
{
return string.Empty;
}
try
{
//添加模板绑定数据
var vltContext = new VelocityContext(BaseContext);
foreach (var pair in obj)
{
vltContext.Put(pair.Key, pair.Value);
}
//返回模板渲染结果
StringWriter vltWriter = new StringWriter();
Velocity.Evaluate(vltContext, vltWriter, null, templ);
return vltWriter.GetStringBuilder().ToString();
}
catch (Exception e)
{
Log.Error(e);
return string.Empty;
}
}
}
}