NVelocity 学习笔记


简单的使用,不含任何讲解

C#代码

using Commons.Collections;
using NVelocity;
using NVelocity.App;
using NVelocity.Runtime;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NVelocityApp1
{
    class Program
    {
        static void Main(string[] args)
        {

            DataTable dt1 = new DataTable();
            dt1.Columns.Add("ID", typeof(string));
            dt1.Columns.Add("Name", typeof(string));
            dt1.Columns.Add("Add", typeof(string));
            dt1.Rows.Add("001", "小明", "我家住黄土高坡上");
            dt1.Rows.Add("002", "小红", "我住在黄土高坡下");

            DataTable dt2 = new DataTable();
            dt2.Columns.Add("ID", typeof(string));
            dt2.Columns.Add("Name", typeof(string));
            dt2.Columns.Add("Score", typeof(string));
            dt2.Rows.Add("001", "数学", 11);
            dt2.Rows.Add("001", "语文", 12);
            dt2.Rows.Add("001", "英语", 13);
            dt2.Rows.Add("002", "数学", 21);
            dt2.Rows.Add("002", "语文", 22);
            dt2.Rows.Add("002", "英语", 23);

            Tools tools = new Tools();

            VelocityEngine vltEngine = new VelocityEngine();//模板引擎实例化
            ExtendedProperties ep = new ExtendedProperties();//模板引擎参数实例化
            ep.AddProperty(RuntimeConstants.RESOURCE_LOADER, "file");//指定资源的加载类型
            ep.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, "E:\\NVelocityTemplate");//指定资源的加载路径   
            ep.AddProperty(RuntimeConstants.INPUT_ENCODING, "gbk");//输出格式
            ep.AddProperty(RuntimeConstants.OUTPUT_ENCODING, "gbk");//输出格式
            vltEngine.Init(ep);

            Template template = vltEngine.GetTemplate("Temp.txt");//加载模板
            VelocityContext vltContext = new VelocityContext(); //当前的数据信息载体集合
            vltContext.Put("Table1", dt1);
            vltContext.Put("Table2", dt2);
            vltContext.Put("Tools", tools);

            StringWriter vltWriter = new StringWriter();
            template.Merge(vltContext, vltWriter);

            Console.Write(vltWriter.ToString());

            Console.ReadLine();

        }
    }

    public class Tools
    {
        public static string Process(string content, int startIndex, int length)
        {
            string result = content.Substring(startIndex, length);
            return result;
        }
    }

}

模板文件

我是开头

#foreach($user1 in $Table1.Rows)
    $user1.Name
    $Tools.Process($user1.Add, 0, 7)
    #foreach($user2 in $Table2.Rows)
        #if($user2.ID == $user1.ID)
            $user2.Name $user2.Score
        #end
    #end

    --------------------我是记录分隔符--------------
#end

我是结尾

操作结果图

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

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);
        }
    }

}

>

#foreach($u in $AboutList)
<a href='http://www.cmono.net'>$MyClass.GetContent($u.Content,3)</a>
#end

知识共享许可协议
《NVelocity 学习笔记》 常伟华 创作。
本作品采用知识共享署名-相同方式共享 4.0 国际许可协议 | 3.0 中国大陆许可协议进行许可。

站内公告

A PHP Error was encountered

Severity: Core Warning

Message: PHP Startup: zip: Unable to initialize module Module compiled with module API=20060613 PHP compiled with module API=20090626 These options need to match

Filename: Unknown

Line Number: 0

Backtrace: