Common/CommonHelper.cs
(完整代码) 1KBusing System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using NVelocity.App;
using NVelocity.Runtime;
using NVelocity;
namespace Common
{
public class CommonHelper
{
/// <summary>
/// 用data数据填充templateName模板,渲染生成html返回
/// </summary>
/// <param name="templateName"></param>
/// <param name="data"></param>
/// <returns></returns>
public static string RenderHtml(string templateName,Dictionary<string,object> tempKeyByValue)
{
VelocityEngine vltEngine = new VelocityEngine();
vltEngine.SetProperty(RuntimeConstants.RESOURCE_LOADER, "file");
vltEngine.SetProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, System.Web.Hosting.HostingEnvironment.MapPath("~/temp"));//模板文件所在的文件夹
vltEngine.Init();
VelocityContext vltContext = new VelocityContext();
foreach (string tempName in tempKeyByValue.Keys)
{
vltContext.Put(tempName, tempKeyByValue[tempName]);//设置参数,在模板中可以通过$data来引用
}
Template vltTemplate = vltEngine.GetTemplate(templateName);
System.IO.StringWriter vltWriter = new System.IO.StringWriter();
vltTemplate.Merge(vltContext, vltWriter);
string html = vltWriter.GetStringBuilder().ToString();
return html;
}
}
}
24 小时内免费解锁 3 个项目,之后 1 算力/个。 规则说明
AI 解读 AI
登录后可用,每次 10 算力,解读结果公开显示在下面。
还没有人解读过这个文件。