zoukankan      html  css  js  c++  java
  • NVelocity练习

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

    using System.IO;
    using Commons.Collections;
    using NVelocity;
    using NVelocity.App;
    using NVelocity.Context;
    using NVelocity.Runtime;


    namespace TestConsoleAppCastleNVelocity
    {
        
    class Program
        {
            
    static void Main(string[] args)
            {
                
    //Step 1 - Creating a VelocityEngine
                VelocityEngine engine = new VelocityEngine();
                ExtendedProperties props 
    = new ExtendedProperties();
                props.AddProperty(RuntimeConstants.RESOURCE_LOADER, 
    "file");
                props.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH,
                    
    "E:/_Projects/DotNet/TestConsoleAppCastleNVelocity/TestConsoleAppCastleNVelocity/templates/");
                props.AddProperty(RuntimeConstants.INPUT_ENCODING, 
    "utf-8");
                props.AddProperty(RuntimeConstants.OUTPUT_ENCODING, 
    "utf-8");
                engine.Init(props);

                
    //Step 2 - Creating the Template            
                Template template = engine.GetTemplate("First.vm");

                
    //Step 3 - 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("顾客姓名吴XX"));

                List
    <Customer> list = new List<Customer>();
                list.Add(
    new Customer("张三"));
                list.Add(
    new Customer("李四"));
                list.Add(
    new Customer("五五"));
                list.Add(
    new Customer("赵六"));
                list.Add(
    new Customer("李七"));
                context.Put(
    "customerList", list);

                StringWriter writer 
    = new StringWriter();
                template.Merge(context, writer);
                Console.WriteLine( writer.GetStringBuilder().ToString() );

                
    //Console.WriteLine( System.AppDomain.CurrentDomain.BaseDirectory );

                Console.ReadKey();
            }
        }
    }

    模板代码:

    代码
    From来自: $from
    To去: $to
    Subject主题: $subject

    Hello $customer.Name

    We
    're please to yada yada yada.
    #set($myInt=1024)
    ${myInt}s

    ****************************
    #set ( $bFlag=3 )
    #
    if($bFlag==1) 
    It's one
    #elseif($bFlag==2)
    It's two
    #else
    It's three
    #end

    ##这是注释

    #*
    这是多行
    注释
    *#
    *******foreach test********
    <table>
    #foreach( $customer in $customerList )
        <tr><td>$velocityCount 
        
    #if($velocityCount%2==0) 偶数行 #else 奇数行 #end
        </td><td>$customer.Name</td></tr>
    #end
    </table>

    /Files/wucg/TestConsoleAppCastleNVelocity.rar

  • 相关阅读:
    WAVECOM CDMA Modem 发送短信
    【转】关于正则表达式匹配任意字符(包括换行符)的写法
    MS2000 差异备份 还原
    推荐一款非常适用的弹框 phpcms v9都用的这个!!!!
    正则截取内容
    javascript面向对象编程实现
    一次 全部删除MSSQL数据库用户表,存储过程
    【转】 jQuery图片预加载+等比例缩放
    多种多样的Loading特效
    关于图片轮播的几种思路
  • 原文地址:https://www.cnblogs.com/wucg/p/1785654.html
Copyright © 2011-2022 走看看