首页 新闻 赞助 找找看

NVelocity 如何传递参数调用自定义方法

0
悬赏园豆:20 [已解决问题] 解决于 2011-11-30 18:49

NVelocity  如何传递参数调用自定义方法

请能告诉我吗,在线等答案

把完整的代码发到我的邮箱哦。huang_34@163.com

谢谢啊。

比如说: 

public static string GetContent(string content, int SubLength)
     {
          return "";

     }

 

在html页面里,如何调用呢

 #foreach($u in $AboutList)
<a href='http://www.baidu.com'>GetContent($u.Content,0)</a>
#end  

 

无法调用,大家告诉我怎么调用自定义的方法。另外给我完整的代码。好吗。

穆桂英的主页 穆桂英 | 初学一级 | 园豆:176
提问于:2011-11-30 13:52
< >
分享
最佳答案
0

你没把方法所在的对象传进去怎么能找得到呢?源代码发到你的邮箱了,你看看吧。另外我把代码放在这里,希望能帮到其它有同样疑问的人吧。

前提,引用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
收获园豆:20
LCM | 大侠五级 |园豆:6876 | 2011-11-30 16:34

谢谢你啊。原来这么简单啊。

NVelocity啥这么帅,啥都可以解决。

穆桂英 | 园豆:176 (初学一级) | 2011-11-30 18:49

感谢啊,还真一通万通

mrcoolye | 园豆:64 (初学一级) | 2012-06-29 10:37
其他回答(1)
0

感谢啊,还真一通万通

mrcoolye | 园豆:64 (初学一级) | 2012-06-29 10:37
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册