zoukankan      html  css  js  c++  java
  • NVelocity模板引擎,初级体验,非常有用的东东.(转)

    NVelocity的使用。
    首先:在http://nvelocity.sourceforge.net/上下载一个 NVelocity-0.4.1.zip
    在其下的bin目录中可找到NVelocity.dll(NET项目中将用到),引入项目中....
    先要引入以下名称空间:
    using Commons.Collections;
    using NVelocity;
    using NVelocity.App;
    using NVelocity.Context;
    第一步:Creating a VelocityEngine也就是创建一个VelocityEngine的实例
    VelocityEngine velocity = new VelocityEngine(); //也可以使用带参构造函数直接实例。
    ExtendedProperties props = new ExtendedProperties();
    velocity.Init(props);

    第二步:Creating the Template加载模板文件
    这时通过的是Template类,并使用VelocityEngine的GetTemplate方法加载模板
    Template template = velocity.GetTemplate(@"path/to/myfirsttemplate.vm");

    第三步:Merging the template整合模板
    VelocityContext context = new VelocityContext();
    context.Put("from", "somewhere");
    context.Put("to", "someone");
    context.Put("subject", "Welcome to NVelocity");
    context.Put("customer", new Customer("John Doe") );

    第四步:创建一个IO流来输出模板内容。推荐使用StringWriter(因为template中以string形式存放)
    StringWriter writer = new StringWriter();
    template.Merge(context, writer);
    Response.Write(writer.ToString());

    ---通过上述步骤就可以轻松的使用NVelocity模板引擎的技术了。
    有没有发现最后的Response.Write(writer.ToString())?
  • 相关阅读:
    Kotlin之类属性延迟初始化
    Android 之ANR
    Android之Handler基础篇
    Android Handler进阶篇
    Android 进程与线程管理
    Android 启动模式LaunchMode详解(LaunchMode四种模式详解)
    Android 应用版本号配置修改
    Android ViewGroup
    Android app与Activity主题配置
    Android 本地序列化
  • 原文地址:https://www.cnblogs.com/aaa6818162/p/1453153.html
Copyright © 2011-2022 走看看