• NVelocity 在.Net的三种用法


    NVelocity 使用文件型模板例子

    using NVelocity;
    using NVelocity.App;
    using NVelocity.Runtime;
    VelocityEngine vltEngine = new VelocityEngine();
    vltEngine.SetProperty(RuntimeConstants.RESOURCE_LOADER, "file");
    vltEngine.SetProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, Server.MapPath("~/Template/"));
    vltEngine.Init();
    VelocityContext vltContext = new VelocityContext();
    vltContext.Put("PageTitle", "文件模板例子");
    vltContext.Put("ListUsers", listUsers);
    Template vltTemplate = vltEngine.GetTemplate("Default.htm");
    System.IO.StringWriter vltWriter = new System.IO.StringWriter();
    vltTemplate.Merge(vltContext, vltWriter);
    Response.Write(vltWriter.GetStringBuilder().ToString());

    NVelocity 使用资源文件模板例子

    using NVelocity;
    using NVelocity.App;
    using NVelocity.Runtime;
            IList<string> listAssembly = new List<string>();
    // 添加程序集名称
            listAssembly.Add("LibTest");
            VelocityEngine vltEngine = new VelocityEngine();
            vltEngine.SetProperty(RuntimeConstants.RESOURCE_LOADER, "assembly");
            vltEngine.SetProperty("assembly.resource.loader.class", "NVelocity.Runtime.Resource.Loader.AssemblyResourceLoader; NVelocity"); // 固定写法- -!
            vltEngine.SetProperty("assembly.resource.loader.assembly", listAssembly);
            vltEngine.Init();
            VelocityContext vltContext = new VelocityContext();
            vltContext.Put("PageTitle", "资源模板例子");
            vltContext.Put("ListUsers", listUsers);
            Template vltTemplate = vltEngine.GetTemplate("LibTest.Resources.Default.htm");
            System.IO.StringWriter vltWriter = new System.IO.StringWriter();
            vltTemplate.Merge(vltContext, vltWriter);
            Response.Write(vltWriter.GetStringBuilder().ToString());

    NVelocity 使用字符串模板例子

    using NVelocity;
    using NVelocity.App;
    using NVelocity.Runtime;
    System.Text.StringBuilder builder = new System.Text.StringBuilder();
            builder.Append("#foreach($u in $ListUsers) " +
    "#beforeall " +
    "<table border="0" cellpadding="10" cellspacing="10">" +
    "<tr><td>Name</td><td>Sex</td><td>City</td></tr>" +
    "#each " +
    "<tr>" +
    "<td>$u.Name</td>" +
    "<td>$u.Sex</td>" +
    "<td>$u.City</td>" +
    "</tr>" +
    "#afterall " +
    "</table>" +
    "#nodata " +
    "暂无用户资料 " +
    "#end");
            VelocityEngine vltEngine = new VelocityEngine();
            vltEngine.Init();
            VelocityContext vltContext = new VelocityContext();
            vltContext.Put("PageTitle", "字符串模板例子");
            vltContext.Put("ListUsers", listUsers);
            System.IO.StringWriter vltWriter = new System.IO.StringWriter();
            vltEngine.Evaluate(vltContext, vltWriter, null, builder.ToString());
            Response.Write(vltWriter.GetStringBuilder().ToString());

  • 相关阅读:
    开始学习编写用于 Windows SideShow 设备的小工具【转】
    Windows Mobile 6.5 Developer Tool Kit 下载
    Microsoft Security Essentials 微软免费杀毒软件下载
    SQL Server 2008 空间数据存储摘抄(SRID 点 MultiPoint LineString MultiLineString 多边形 MultiPolygon GeometryCollection)
    Vista Sidebar Gadget (侧边栏小工具)开发教程 (2)
    Vista Sidebar Gadget (侧边栏小工具)开发教程 (4)
    负载测试、压力测试和性能测试的异同
    Windows Server 2008 Vista Sidebar Gadget (侧边栏小工具) 入门开发实例
    Silverlight Tools 安装失败 解决办法
    SQL Server 2008 空间数据库 空间索引概念及创建(取自帮助)
  • 原文地址:https://www.cnblogs.com/chencidi/p/3643064.html
Copyright © 2020-2023  润新知