本文整理匯總了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;
}