zoukankan      html  css  js  c++  java
  • .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());

    例子下载

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

    例子下载

    using NVelocity;
    using NVelocity.App;
    using NVelocity.Runtime;

    System.Text.StringBuilder builder = 
    new System.Text.StringBuilder();
            builder.Append(
    "#foreach($u in $ListUsers)"r"n" +
                
    "#beforeall"r"n" +
                
    "<table border=""0"" cellpadding=""10"" cellspacing=""10"">" +
                
    "<tr><td>Name</td><td>Sex</td><td>City</td></tr>" +
                
    "#each"r"n" +
                
    "<tr>" +
                
    "<td>$u.Name</td>" +
                
    "<td>$u.Sex</td>" +
                
    "<td>$u.City</td>" +
                
    "</tr>" +
                
    "#afterall"r"n" +
                
    "</table>" +
                
    "#nodata"r"n" +
                
    "暂无用户资料"r"n" +
                
    "#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());

  • 相关阅读:
    e667. 在给定图像中创建缓冲图像
    e661. 确定图像中是否有透明像素
    e673. Getting Amount of Free Accelerated Image Memory
    e663. 在gif图像中获取透明和色彩的数量
    e662. 取的图像的色彩模型
    e675. 翻转缓冲图像
    e665. 在图像中过滤三元色
    e679. 浮雕化图像
    e669. 绘制缓冲图像
    e664. 在图像中获取子图像
  • 原文地址:https://www.cnblogs.com/aaa6818162/p/1456481.html
Copyright © 2011-2022 走看看