zoukankan      html  css  js  c++  java
  • NVelocity 类操作模板

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    using System.Web;
    using System.IO;
    using NVelocity.App;
    using NVelocity;
    using NVelocity.Runtime;

    namespace Common
    {
    public class NVelocityHelper
    {
    private VelocityEngine velocityEngine = null;
    private VelocityContext vc = null;
    public NVelocityHelper(string templatePath, string key, object value)
    {

    //1.创建Velocity 引擎(VelocityEngine)并设置属性
    velocityEngine = new VelocityEngine();
    velocityEngine.AddProperty(RuntimeConstants.RESOURCE_LOADER, "file"); // Velocity加载类型
    velocityEngine.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, // Velocity加载文件路径
    HttpContext.Current.Server.MapPath("~/" + templatePath + "/"));
    velocityEngine.AddProperty(RuntimeConstants.INPUT_ENCODING, "utf-8"); // 输入编码格式设置
    velocityEngine.AddProperty(RuntimeConstants.OUTPUT_ENCODING, "utf-8"); // 输出编码格式设置
    velocityEngine.Init();
    //2.Velocity 上下文对象设置
    vc = new VelocityContext();
    vc.Put(key, value);
    }

    public NVelocityHelper(string key, object value)
    {
    //1.创建Velocity 引擎(VelocityEngine)并设置属性
    velocityEngine = new VelocityEngine();
    velocityEngine.AddProperty(RuntimeConstants.RESOURCE_LOADER, "file"); // Velocity加载类型
    velocityEngine.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, // Velocity加载文件路径
    HttpContext.Current.Server.MapPath("~/Templates/"));
    velocityEngine.AddProperty(RuntimeConstants.INPUT_ENCODING, "utf-8"); // 输入编码格式设置
    velocityEngine.AddProperty(RuntimeConstants.OUTPUT_ENCODING, "utf-8"); // 输出编码格式设置
    velocityEngine.Init();
    //2.Velocity 上下文对象设置
    vc = new VelocityContext();
    vc.Put(key, value);
    }


    public void Render(string templatFileName)
    {
    //3.创建模板
    Template template = velocityEngine.GetTemplate(templatFileName);

    //4.合并模板和上下文对象、输出
    StringWriter writer = new StringWriter();
    template.Merge(vc, writer);
    HttpContext.Current.Response.Write(writer.ToString());
    HttpContext.Current.Response.End();
    }

    }
    }

  • 相关阅读:
    CSS Grid 布局
    sublime text 3配置c/c++编译环境
    [工具/PC]计算机中丢失libiconv-2.dll,丢失libintl-8.dll,无法定位程序输入点libiconv于动态链接库libiconv-2.dll上问题解决方法
    操作系统Unix、Windows、Mac OS、Linux的故事
    编译型语言与解释型语言的区别及各自的优缺点
    node版本控制之nvm
    Webpack devServer中的 proxy 实现跨域
    webpack-dev-server配置指南(使用webpack3.0)
    [bzoj3295][Cqoi2011]动态逆序对_主席树
    [bzoj2989]数列_KD-Tree_旋转坐标系
  • 原文地址:https://www.cnblogs.com/lierjie/p/3748045.html
Copyright © 2011-2022 走看看