NVelocity 如何传递参数调用自定义方法
展开全部
前提,引用NVelocity.dll
然后见代码,
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.textBox1.Text = GetString("a");
}
/// <summary>
/// 通过模板名称,获取内容字符串
/// </summary>
/// <param name="templateName"></param>
/// <returns></returns>
public String GetString(String templateName)
{
NVelocity.App.VelocityEngine vltEngine = new NVelocity.App.VelocityEngine();
vltEngine.SetProperty(NVelocity.Runtime.RuntimeConstants.RESOURCE_LOADER, "file");
vltEngine.SetProperty(NVelocity.Runtime.RuntimeConstants.FILE_RESOURCE_LOADER_PATH, System.AppDomain.CurrentDomain.BaseDirectory);
vltEngine.Init();
StringWriter writer = new StringWriter();
if (!string.IsNullOrEmpty(templateName))
{
String fileName = templateName + ".vm";
NVelocity.Template temp = vltEngine.GetTemplate(fileName, "utf-8");
temp.Merge(GetContext(), writer);
return writer.ToString();
}
return "";
}
private NVelocity.VelocityContext GetContext()
{
NVelocity.VelocityContext velocityContext = new NVelocity.VelocityContext();
List<About> AboutList=new List<About>();
About about=new About();
about.Content = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
AboutList.Add(about);
about = new About();
about.Content = "bb";
AboutList.Add(about);
about = new About();
about.Content = "cccccccccccccc";
AboutList.Add(about);
about = new About();
about.Content = "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee";
AboutList.Add(about);
//要把传给模板的对象Put到Context中
velocityContext.Put("AboutList", AboutList);
//要把模板中调用的方法的对象也Put到Context中. 好像不能调用静态方法
velocityContext.Put("MyClass", new MyClass());
return velocityContext;
}
}
//这个是测试用的实体,传到模板中赋值用的
public class About
{
public string Content { get; set; }
}
/// <summary>
/// 这里面的方法就是我要调用的
/// </summary>
public class MyClass
{
public string GetContent(string content, int SubLength)
{
if (string.IsNullOrEmpty(content) || content.Length < SubLength)
return content;
return content.Substring(0,SubLength);
}
}
}
模板文件如下,这个测试示例中名称我写成a.vm
#foreach($u in $AboutList)
<a href='http://www.baidu.com'>$MyClass.GetContent($u.Content,3)</a>
#end
然后见代码,
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.textBox1.Text = GetString("a");
}
/// <summary>
/// 通过模板名称,获取内容字符串
/// </summary>
/// <param name="templateName"></param>
/// <returns></returns>
public String GetString(String templateName)
{
NVelocity.App.VelocityEngine vltEngine = new NVelocity.App.VelocityEngine();
vltEngine.SetProperty(NVelocity.Runtime.RuntimeConstants.RESOURCE_LOADER, "file");
vltEngine.SetProperty(NVelocity.Runtime.RuntimeConstants.FILE_RESOURCE_LOADER_PATH, System.AppDomain.CurrentDomain.BaseDirectory);
vltEngine.Init();
StringWriter writer = new StringWriter();
if (!string.IsNullOrEmpty(templateName))
{
String fileName = templateName + ".vm";
NVelocity.Template temp = vltEngine.GetTemplate(fileName, "utf-8");
temp.Merge(GetContext(), writer);
return writer.ToString();
}
return "";
}
private NVelocity.VelocityContext GetContext()
{
NVelocity.VelocityContext velocityContext = new NVelocity.VelocityContext();
List<About> AboutList=new List<About>();
About about=new About();
about.Content = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
AboutList.Add(about);
about = new About();
about.Content = "bb";
AboutList.Add(about);
about = new About();
about.Content = "cccccccccccccc";
AboutList.Add(about);
about = new About();
about.Content = "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee";
AboutList.Add(about);
//要把传给模板的对象Put到Context中
velocityContext.Put("AboutList", AboutList);
//要把模板中调用的方法的对象也Put到Context中. 好像不能调用静态方法
velocityContext.Put("MyClass", new MyClass());
return velocityContext;
}
}
//这个是测试用的实体,传到模板中赋值用的
public class About
{
public string Content { get; set; }
}
/// <summary>
/// 这里面的方法就是我要调用的
/// </summary>
public class MyClass
{
public string GetContent(string content, int SubLength)
{
if (string.IsNullOrEmpty(content) || content.Length < SubLength)
return content;
return content.Substring(0,SubLength);
}
}
}
模板文件如下,这个测试示例中名称我写成a.vm
#foreach($u in $AboutList)
<a href='http://www.baidu.com'>$MyClass.GetContent($u.Content,3)</a>
#end
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询