本文整理汇总了C#中NVelocity.VelocityContext类的典型用法代码示例。如果您正苦于以下问题:C# VelocityContext类的具体用法?C# VelocityContext怎么用?C# VelocityContext使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
VelocityContext类属于NVelocity命名空间,在下文中一共展示了VelocityContext类的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: RenderTemplate
public void RenderTemplate(string templateName, object model)
{
string templateFullPath = applicationInfo.AbsolutizePath("Web/Pages/Templates/" + templateName + ".vm.html");
ITemplateSource template = new CacheableFileTemplate(
templateFullPath,
fileCache);
string templateText = template.GetTemplate();
VelocityEngine velocity = new VelocityEngine();
velocity.Init();
VelocityContext velocityContext = new VelocityContext();
velocityContext.Put("m", model);
velocityContext.Put("h", this);
using (StringWriter stringWriter = new StringWriter(CultureInfo.InvariantCulture))
{
if (false == velocity.Evaluate(velocityContext, stringWriter, null, templateText))
throw new InvalidOperationException("Template expansion failed");
writer.InnerWriter.Write(stringWriter.ToString());
}
writer.WriteLine();
}
示例4: FormatText
public static string FormatText(string templateText, IDictionary<string,object> values)
{
var nvelocityContext = new VelocityContext();
foreach (var tagValue in values)
nvelocityContext.Put(tagValue.Key, tagValue.Value);
return FormatText(templateText, nvelocityContext);
}
示例5: RenderView
public static string RenderView(this HtmlHelper html, string viewName, string sectionName, IDictionary parameters)
{
if (!string.IsNullOrEmpty(sectionName))
{
viewName += ":" + sectionName;
}
var velocityView = html.ViewContext.View as NVelocityView;
if (velocityView == null) throw new InvalidOperationException("The RenderView extension can only be used from views that were created using NVelocity.");
var newContext = velocityView.Context;
if (parameters != null && parameters.Keys.Count > 0)
{
// Clone the existing context and then add the custom parameters
newContext = new VelocityContext();
foreach (var key in velocityView.Context.Keys)
{
newContext.Put((string)key, velocityView.Context.Get((string)key));
}
foreach (var key in parameters.Keys)
{
newContext.Put((string)key, parameters[key]);
}
}
// Resolve the template and render it. The partial resource loader takes care of validating
// the view name and extracting the partials.
var template = velocityView.Engine.GetTemplate(viewName);
using (var writer = new StringWriter())
{
template.Merge(newContext, writer);
return writer.ToString();
}
}
示例6: Format
public static string Format(HttpContext context, string pattern, VelocityContext velocitycontext)
{
using (var writer = new StringWriter())
{
try
{
if (!_isInitialized)
{
var props = new ExtendedProperties();
props.AddProperty("file.resource.loader.path",
new ArrayList(new[]
{
".",
Path.Combine(
context.Server.MapPath(feed.HandlerBasePath),
"Patterns")
}));
Velocity.Init(props);
_isInitialized = true;
}
//Load patterns
var template = Patterns.Get(pattern, () => LoadTemplate(pattern));
template.Merge(velocitycontext, writer);
return writer.GetStringBuilder().ToString();
}
catch (Exception)
{
//Format failed some way
return writer.GetStringBuilder().ToString();
}
}
}
示例7: ProcessTemplate
public string ProcessTemplate(string templateValue, Hashtable context)
{
VelocityContext vContext = new VelocityContext(context);
//foreach(string key in context.Keys)
// vContext.Put(key, context[key]);
string result = null;
if (!string.IsNullOrEmpty(templateValue))
{
System.IO.StringWriter writer = new System.IO.StringWriter();
try
{
if (Velocity.Evaluate(vContext, writer, "", templateValue))
result = writer.ToString();
}
catch (ParseErrorException pe)
{
return pe.Message;
}
catch (MethodInvocationException mi)
{
return mi.Message;
}
}
return result;
}
示例8: ExecuteMethodUntilSignal2
/// <summary>
/// This test uses the previously created velocity engine
/// </summary>
public void ExecuteMethodUntilSignal2()
{
startEvent.WaitOne(int.MaxValue, false);
while(!stopEvent.WaitOne(0, false))
{
StringWriter sw = new StringWriter();
VelocityContext c = new VelocityContext();
c.Put("x", new Something());
c.Put("items", items);
bool ok = velocityEngine.Evaluate(c, sw,
"ContextTest.CaseInsensitive",
@"
#foreach($item in $items)
$item,
#end
$x.Print('hey') $x.Contents('test', '1')
");
Assert.IsTrue(ok, "Evaluation returned failure");
Assert.AreEqual("a,b,c,d,heytest,1", Normalize(sw));
}
}
示例9: Format
/// <summary>The format.</summary>
/// <param name="text">The text.</param>
/// <param name="items">The items.</param>
/// <returns>The format.</returns>
/// <exception cref="TemplateException"></exception>
public string Format(string text, Dictionary<string, object> items)
{
try
{
VelocityContext velocityContext = new VelocityContext();
if (items != null)
{
foreach (var pair in items)
{
velocityContext.Put(pair.Key, pair.Value);
}
}
StringWriter sw = new StringWriter();
VelocityEngine velocityEngine = new VelocityEngine();
velocityEngine.Init();
bool ok = velocityEngine.Evaluate(velocityContext, sw, "ContextTest.CaseInsensitive", text);
if (!ok)
{
throw new TemplateException("Template run error (try adding an extra newline at the end of the file)");
}
return sw.ToString();
}
catch (ParseErrorException parseErrorException)
{
throw new TemplateException(parseErrorException.Message, parseErrorException);
}
}
示例10: 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);
}
}
}
示例11: NVelocityView
/// <summary>
/// Initializes a new instance of the <see cref="NVelocityView"/> class.
/// </summary>
/// <param name="template">The template.</param>
/// <param name="viewData">The view data.</param>
/// <param name="engine">The engine.</param>
public NVelocityView(Template template, ViewDataDictionary viewData, VelocityEngine engine)
{
_context = new VelocityContext();
_template = template;
_engine = engine;
ViewData = viewData;
}
示例12: 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();
}
}
示例13: GetGenericTemplateText
protected virtual string GetGenericTemplateText(string templateFileName, Hashtable data) {
string fullTemplateFileName = TemplatePath + templateFileName;
//Check file
if (!IsValidTemplate(fullTemplateFileName)) {
return null;
}
InitialiseNVelocity(templateFileName);
VelocityContext context = new VelocityContext();
foreach (object key in data.Keys) {
AddData(key.ToString(), data[key], context);
}
//Standard things used in email
AddStandardItems(context);
StringWriter writer = new StringWriter();
Template template = null;
try {
template = Velocity.GetTemplate(templateFileName);
template.Merge(context, writer);
return writer.ToString();
} catch (Exception e) {
LogManager.GetLogger(GetType()).Error(e);
}
return null;
}
开发者ID:xcrash,项目名称:cuyahogacontrib-Cuyahoga.Modules.ECommerce,代码行数:35,代码来源:NVelocityTemplateEngine.cs
示例14: CreateCode
public static void CreateCode(string filepath, string outputpath, VelocityContext context)
{
StreamWriter writer=null;
try
{
VelocityEngine engine = null;//new VelocityEngine();
ExtendedProperties extendedProperties = new ExtendedProperties();
//extendedProperties.SetProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, filepath.Substring(0, filepath.LastIndexOf("\\")));
engine.Init(extendedProperties);
//Template template = engine.GetTemplate(filepath.Substring(filepath.LastIndexOf("\\")+1));
//FileStream fos = new FileStream(outputpath + "\\1.vm", FileMode.Create);
//writer = new StreamWriter(fos);
//template.Merge(context, writer);
//writer.Flush();
//writer.Close();
StringWriter output=new StringWriter();
engine.Evaluate(context, output, "", filepath);
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (writer != null)
{
writer.Close();
}
}
}
示例15: BuildEmail
/// <exception cref="ArgumentException"></exception>
string BuildEmail(string templateName, IDictionary<string, object> viewdata)
{
if (viewdata == null)
{
throw new ArgumentNullException("viewData");
}
if (string.IsNullOrEmpty(templateName))
{
throw new ArgumentException("TemplateName");
}
var template = ResolveTemplate(templateName);
var context = new VelocityContext();
foreach (var key in viewdata.Keys)
{
context.Put(key, viewdata[key]);
}
using (var writer = new StringWriter())
{
template.Merge(context, writer);
return writer.ToString();
}
}