本文整理汇总了C#中NVelocity.App.VelocityEngine.GetTemplate方法的典型用法代码示例。如果您正苦于以下问题:C# VelocityEngine.GetTemplate方法的具体用法?C# VelocityEngine.GetTemplate怎么用?C# VelocityEngine.GetTemplate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类NVelocity.App.VelocityEngine
的用法示例。
在下文中一共展示了VelocityEngine.GetTemplate方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ProcessRequest
public void ProcessRequest(HttpContext context)
{
DataBooks book=new DataBooks();
book.name = context.Request["bookname"];
book.type = context.Request["booktype"];
if(book.name!=null)
bookcollector.Add(book);
context.Response.ContentType = "text/html";
VelocityEngine vltEngine = new VelocityEngine();
vltEngine.SetProperty(RuntimeConstants.RESOURCE_LOADER, "file");
vltEngine.SetProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, System.Web.Hosting.HostingEnvironment.MapPath("~/templates"));//模板文件所在的文件夹
vltEngine.Init();
VelocityContext vltContext = new VelocityContext();
//vltContext.Put("msg", "");
vltContext.Put("bookcollector", bookcollector);
vltContext.Put("book", book);
Template vltTemplate = vltEngine.GetTemplate("Front/ShopingCar.html");//模版文件所在位置
System.IO.StringWriter vltWriter = new System.IO.StringWriter();
vltTemplate.Merge(vltContext, vltWriter);
string html = vltWriter.GetStringBuilder().ToString();
context.Response.Write(html);
}
示例2: RenderHtml
/// <summary>
/// 用data数据填充templateName模板,渲染生成html返回
/// </summary>
/// <param name="templateName"></param>
/// <param name="data"></param>
/// <returns></returns>
public static string RenderHtml(string templateName, object data)
{
//第一步:Creating a VelocityEngine也就是创建一个VelocityEngine的实例
VelocityEngine vltEngine = new VelocityEngine(); //也可以使用带参构造函数直接实例
vltEngine.SetProperty(RuntimeConstants.RESOURCE_LOADER, "file");
vltEngine.SetProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, System.Web.Hosting.HostingEnvironment.MapPath("~/templates"));//模板文件所在的文件夹
vltEngine.Init();
//vltEngine.AddProperty(RuntimeConstants.INPUT_ENCODING, "gb2312");
//vltEngine.AddProperty(RuntimeConstants.OUTPUT_ENCODING, "gb2312");
//第二步:Creating the Template加载模板文件
//这时通过的是Template类,并使用VelocityEngine的GetTemplate方法加载模板
Template vltTemplate = vltEngine.GetTemplate(templateName);
//第三步:Merging the template整合模板
VelocityContext vltContext = new VelocityContext();
vltContext.Put("Data", data);//设置参数,在模板中可以通过$data来引用
//第四步:创建一个IO流来输出模板内容推荐使用StringWriter(因为template中以string形式存放)
System.IO.StringWriter vltWriter = new System.IO.StringWriter();
vltTemplate.Merge(vltContext, vltWriter);
string html = vltWriter.GetStringBuilder().ToString();
return html;
}
示例3: ProcessRequest
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/html";
string username = context.Request.Form["username"];
string password = context.Request.Form["password"];
if (string.IsNullOrEmpty(username) && string.IsNullOrEmpty(password))
{
VelocityEngine vltEngine = new VelocityEngine();
vltEngine.SetProperty(RuntimeConstants.RESOURCE_LOADER, "file");
vltEngine.SetProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, System.Web.Hosting.HostingEnvironment.MapPath("~/templates"));//模板文件所在的文件夹
vltEngine.Init();
VelocityContext vltContext = new VelocityContext();
//vltContext.Put("p", person);//设置参数,在模板中可以通过$data来引用
vltContext.Put("username", "");
vltContext.Put("password", "");
vltContext.Put("msg", "");
Template vltTemplate = vltEngine.GetTemplate("Front/login.html");//模版文件所在位置
System.IO.StringWriter vltWriter = new System.IO.StringWriter();
vltTemplate.Merge(vltContext, vltWriter);
string html = vltWriter.GetStringBuilder().ToString();
context.Response.Write(html);
}
else
{
if (dataaccess(username, password))
{
context.Session["username"] = username;
context.Response.Redirect("Index.ashx");
}
else
{
VelocityEngine vltEngine = new VelocityEngine();
vltEngine.SetProperty(RuntimeConstants.RESOURCE_LOADER, "file");
vltEngine.SetProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, System.Web.Hosting.HostingEnvironment.MapPath("~/templates"));//模板文件所在的文件夹
vltEngine.Init();
VelocityContext vltContext = new VelocityContext();
//vltContext.Put("p", person);//设置参数,在模板中可以通过$data来引用
vltContext.Put("username", username);
vltContext.Put("password", password);
vltContext.Put("msg", "用户名密码错误");
Template vltTemplate = vltEngine.GetTemplate("Front/login.html");//模版文件所在位置
System.IO.StringWriter vltWriter = new System.IO.StringWriter();
vltTemplate.Merge(vltContext, vltWriter);
string html = vltWriter.GetStringBuilder().ToString();
context.Response.Write(html);
}
}
}
示例4: RenderTemplate
public string RenderTemplate(string masterPage, string templateName, IDictionary<string, object> data)
{
if (string.IsNullOrEmpty(templateName))
{
throw new ArgumentException("The \"templateName\" parameter must be specified", "templateName");
}
var name = !string.IsNullOrEmpty(masterPage)
? masterPage : templateName;
var engine = new VelocityEngine();
var props = new ExtendedProperties();
props.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, _templatesPath);
engine.Init(props);
var template = engine.GetTemplate(name);
template.Encoding = Encoding.UTF8.BodyName;
var context = new VelocityContext();
var templateData = data ?? new Dictionary<string, object>();
foreach (var key in templateData.Keys)
{
context.Put(key, templateData[key]);
}
if (!string.IsNullOrEmpty(masterPage))
{
context.Put("childContent", templateName);
}
using (var writer = new StringWriter())
{
engine.MergeTemplate(name, context, writer);
return writer.GetStringBuilder().ToString();
}
}
示例5: readerHtml
public static string readerHtml(string path, 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(path));//模板文件所在的文件夹
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();
}
示例6: Test
public void Test()
{
var vlte = new VelocityEngine();
var props = new ExtendedProperties();
props.AddProperty(RuntimeConstants.RESOURCE_LOADER, "file");
props.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, AppDomain.CurrentDomain.BaseDirectory);
vlte.Init(props);
var vlc = new VelocityContext();
vlc.Put("test", "test");
var vtp = vlte.GetTemplate("NVelocityTest.vm");
var str = new StringWriter();
vtp.Merge(vlc, str);
Console.WriteLine(str.GetStringBuilder().ToString());
}
示例7: ProcessRequest
public void ProcessRequest(HttpContext context)
{
string searchid= context.Request.Form["searchid"];
string searchtext = context.Request.Form["searchtext"];
dataaccess(searchid, searchtext);
if (searchid == "2")
{
//book
context.Response.ContentType = "text/html";
VelocityEngine vltEngine = new VelocityEngine();
vltEngine.SetProperty(RuntimeConstants.RESOURCE_LOADER, "file");
vltEngine.SetProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, System.Web.Hosting.HostingEnvironment.MapPath("~/templates"));//模板文件所在的文件夹
vltEngine.Init();
VelocityContext vltContext = new VelocityContext();
vltContext.Put("object", searchtext);
string show="<img src='books/"+book.ser+"' width='210' height='150' />";
vltContext.Put("result", show);
Template vltTemplate = vltEngine.GetTemplate("Front/Search.html");//模版文件所在位置
System.IO.StringWriter vltWriter = new System.IO.StringWriter();
vltTemplate.Merge(vltContext, vltWriter);
string html = vltWriter.GetStringBuilder().ToString();
context.Response.Write(html);
}
else if (searchid == "3")
{
//news
context.Response.ContentType = "text/html";
VelocityEngine vltEngine = new VelocityEngine();
vltEngine.SetProperty(RuntimeConstants.RESOURCE_LOADER, "file");
vltEngine.SetProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, System.Web.Hosting.HostingEnvironment.MapPath("~/templates"));//模板文件所在的文件夹
vltEngine.Init();
VelocityContext vltContext = new VelocityContext();
vltContext.Put("object", searchtext);
vltContext.Put("result", news.news);
Template vltTemplate = vltEngine.GetTemplate("Front/Search.html");//模版文件所在位置
System.IO.StringWriter vltWriter = new System.IO.StringWriter();
vltTemplate.Merge(vltContext, vltWriter);
string html = vltWriter.GetStringBuilder().ToString();
context.Response.Write(html);
}
}
示例8: FillResponseTemplate
public virtual string FillResponseTemplate(IDictionary<string, object> viewParams)
{
var engine = new VelocityEngine();
var props = new ExtendedProperties();
props.AddProperty(RuntimeConstants.RESOURCE_LOADER, "assembly");
props.AddProperty("assembly.resource.loader.class",
"NVelocity.Runtime.Resource.Loader.AssemblyResourceLoader, NVelocity");
props.AddProperty("assembly.resource.loader.assembly", GetType().Assembly.FullName);
engine.Init(props);
var vcontext = new VelocityContext();
foreach (var k in viewParams) {
vcontext.Put(k.Key, k.Value);
}
using (var writer = new StringWriter()) {
engine.GetTemplate(ViewName).Merge(vcontext, writer);
return writer.GetStringBuilder().ToString();
}
}
示例9: ProcessRequest
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/html";
//创建一个模板引擎
VelocityEngine vltEngine = new VelocityEngine();
//文件型模板,还可以是 assembly ,则使用资源文件
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("Title", "标题"); //设置参数,在模板中可以通过$Title来引用
vltContext.Put("Body", "内容"); //设置参数,在模板中可以通过$Body来引用
vltContext.Put("Date", DateTime.Now); //设置参数,在模板中可以通过$Date来引用
//获取我们刚才所定义的模板,上面已设置模板目录
Template vltTemplate = vltEngine.GetTemplate("basic.html");
System.IO.StringWriter vltWriter = new System.IO.StringWriter();
//根据模板的上下文,将模板生成的内容写进刚才定义的字符串输出流中
vltTemplate.Merge(vltContext, vltWriter);
string html = vltWriter.GetStringBuilder().ToString();
context.Response.Write(html);
////字符串模板源,这里就是你的邮件模板等等的字符串
//const string templateStr = "$Title,$Body,$Date";
////创建一个模板引擎
//VelocityEngine vltEngine = new VelocityEngine();
////文件型模板,还可以是 assembly ,则使用资源文件
//vltEngine.SetProperty(RuntimeConstants.RESOURCE_LOADER, "file");
//vltEngine.Init();
////定义一个模板上下文
//VelocityContext vltContext = new VelocityContext();
////传入模板所需要的参数
//vltContext.Put("Title", "标题"); //设置参数,在模板中可以通过$Title来引用
//vltContext.Put("Body", "内容"); //设置参数,在模板中可以通过$Body来引用
//vltContext.Put("Date", DateTime.Now); //设置参数,在模板中可以通过$Date来引用
////定义一个字符串输出流
//StringWriter vltWriter = new StringWriter();
////输出字符串流中的数据
//vltEngine.Evaluate(vltContext, vltWriter, null, templateStr);
//context.Response.Write(vltWriter.GetStringBuilder().ToString());
}
示例10: Generate
/// <summary>
/// Generates a report filled with the content supplied by <paramref name="report"/>.
/// </summary>
/// <param name="report">Specifies the report model.</param>
public void Generate(IReport report)
{
var engine = new VelocityEngine();
var properties = new ExtendedProperties();
properties.AddProperty("resource.loader", "assembly");
properties.AddProperty("resource.manager.class", typeof(ResourceManagerImpl).AssemblyQualifiedName);
properties.AddProperty("directive.manager", EncodeExternalAssemblyQualifiedType(typeof(DirectiveManagerProxy)));
properties.AddProperty("assembly.resource.loader.class", typeof(AssemblyResourceLoader).AssemblyQualifiedName);
properties.AddProperty("assembly.resource.loader.assembly", GetType().Assembly.GetName().Name);
engine.Init(properties);
var htmlTemplate = engine.GetTemplate("Xunit.Reporting.Internal.Generator.HtmlTemplate.vm");
var generationContext = new VelocityContext();
generationContext.Put("report", report);
generationContext.Put("pluralizer", new Pluralizer());
_outputWriter.Write(
string.Concat(report.ReflectedAssembly, ".html"),
writer => htmlTemplate.Merge(generationContext, writer));
}
示例11: GetCodeFileString
public static string GetCodeFileString(HttpServerUtility server, CustomItemInformation info)
{
VelocityEngine velocity = new VelocityEngine();
ExtendedProperties props = new ExtendedProperties();
props.SetProperty("file.resource.loader.path", server.MapPath(".")); // The base path for Templates
velocity.Init(props);
//Template template = velocity.GetTemplate("template.tmp");
NVelocity.Template template = velocity.GetTemplate("CustomItem.vm");
VelocityContext context = new VelocityContext();
context.Put("BaseTemplates", info.BaseTemplates);
context.Put("CustomItemFields", info.Fields);
context.Put("CustomItemInformation", info);
StringWriter writer = new StringWriter();
template.Merge(context, writer);
return writer.GetStringBuilder().ToString();
}
示例12: ProcessRequest
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/html";
VelocityEngine vltEngine = new VelocityEngine();
vltEngine.SetProperty(RuntimeConstants.RESOURCE_LOADER, "file");
vltEngine.SetProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, System.Web.Hosting.HostingEnvironment.MapPath("~/templates"));//模板文件所在的文件夹
vltEngine.Init();
VelocityContext vltContext = new VelocityContext();
//vltContext.Put("msg", "");
dataaccess();
vltContext.Put("NewsPool", NewsPool);
vltContext.Put("s", new GotNews());
Template vltTemplate = vltEngine.GetTemplate("Front/News.html");//模版文件所在位置
System.IO.StringWriter vltWriter = new System.IO.StringWriter();
vltTemplate.Merge(vltContext, vltWriter);
string html = vltWriter.GetStringBuilder().ToString();
context.Response.Write(html);
}
示例13: GetFileText
/// <summary>
/// 通过模版文件路径读取文件内容
/// </summary>
/// <param name="path">模版文件路径</param>
/// <param name="ht">模版文件的参数</param>
/// <returns>StringWriter对象</returns>
public static StringWriter GetFileText(string path, Hashtable ht)
{
if (String.IsNullOrEmpty(path))
{
throw new ArgumentNullException("模版文件路径为空!");
}
try
{
string tmpPath = path.Substring(0,path.LastIndexOf(@"\"));
string filePath = path.Substring(path.LastIndexOf(@"\")+1);
//创建NVelocity引擎的实例对象
VelocityEngine velocity = new VelocityEngine();
ExtendedProperties props = new ExtendedProperties();
props.AddProperty(RuntimeConstants.RESOURCE_LOADER, "file");
props.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, tmpPath);
props.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_CACHE, true);
props.AddProperty(RuntimeConstants.INPUT_ENCODING, "utf-8");
props.AddProperty(RuntimeConstants.OUTPUT_ENCODING, "utf-8");
velocity.Init(props);
//从文件中读取模板
Template temp = velocity.GetTemplate(filePath);
IContext context = new VelocityContext();
foreach (string key in ht.Keys)
{
context.Put(key, ht[key]);
}
//合并模板
StringWriter writer = new StringWriter();
temp.Merge(context, writer);
return writer;
}
catch (Exception ex)
{
throw ex;
}
}
示例14: ProcessRequest
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/html";
VelocityEngine velocityEngine = new VelocityEngine();
velocityEngine.SetProperty(RuntimeConstants.RESOURCE_LOADER, "file");
velocityEngine.SetProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, System.Web.Hosting.HostingEnvironment.MapPath("/template"));
velocityEngine.Init();
//定义一个模板上下文
VelocityContext velocityContext = new VelocityContext();
//定义类对象
Person p = new Person()
{
Name = "王三",
Age = 20
};
velocityContext.Put("p", p);
//定义键值对
Dictionary<string,string> dic=new Dictionary<string, string>();
dic["aa"] = "李四";
dic["bb"] = "30";
velocityContext.Put("dic", dic);
//定义list集合
List<string> list=new List<string>(){"张五","马六"};
velocityContext.Put("list", list);
//获取定义的模板
Template template = velocityEngine.GetTemplate("baseobject.html");
StringWriter stringWriter = new StringWriter();
template.Merge(velocityContext, stringWriter);
string html = stringWriter.GetStringBuilder().ToString();
context.Response.Write(html);
}
示例15: InitializeTask
protected override void InitializeTask(XmlNode taskNode)
{
base.InitializeTask(taskNode);
// Initializes NVelocity
velocity = new VelocityEngine();
ExtendedProperties props = new ExtendedProperties();
velocity.Init(props);
template = velocity.GetTemplate(templateFile);
// TODO: validate all arguments are present
counter = startOrder + 1;
}