• Velocity简单语法及VelocityHelper封装


    1.简单替换
    ##这是注释
    Wellcome ${userName}! Now:$date

    2.申明变量:
    #set( $iAmVariable = "good!" )
    Welcome $name to Javayou.com!
    today is $date.
    $iAmVariable

    3.if语句:
    #set ($admin = "admin")
    #set ($user = "user")
    #if ($admin == $user)
    Welcome admin!
    #else
    Welcome user!
    #end

    4.遍历对象:
    #foreach( $product in $list )
    <li>$product</li>
    #end

    5.自定义对象:
    #foreach( $s in $students )
    <$velocityCount> No:$s.No, Address: $s.Address

    #end


    6.标签嵌套:
    #foreach ($element in $list) 
    --外部循环-- 
        $velocityCount:This is $element.
    --内部循环--
    #foreach ($element in $list)
          $velocityCount:This is $element.
    #end
    --内部循环--
    --外部循环--
    #end

    7.调用自定义对象方法:
    #foreach( $s in $students )
    <$velocityCount> $s.SayHello();

    #end

    [csharp] view plain copy
     
     print?
    1. using System.IO;  
    2. using NVelocity.App;  
    3. using NVelocity.Context;  
    4. using NVelocity.Runtime;  
    5.   
    6. namespace NVelocity  
    7. {  
    8.     /// <summary>  
    9.     ///     NVelocity模板工具类 VelocityHelper  
    10.     /// </summary>  
    11.     public class VelocityHelper  
    12.     {  
    13.         private IContext _context;  
    14.         private VelocityEngine _velocity;  
    15.         private string _templateName;  
    16.   
    17.         /// <summary>  
    18.         ///     构造函数  
    19.         /// </summary>  
    20.         /// <param name="templatDir">模板文件夹路径</param>  
    21.         /// <param name="templateName">模板文件名</param>  
    22.         public VelocityHelper(string templatDir, string templateName)  
    23.         {  
    24.             Init(templatDir);  
    25.             _templateName = templateName;  
    26.         }  
    27.   
    28.         /// <summary>  
    29.         ///     无参数构造函数  
    30.         /// </summary>  
    31.         public VelocityHelper()  
    32.         {  
    33.         }  
    34.   
    35.         /// <summary>  
    36.         /// 设置模板文件夹  
    37.         /// </summary>  
    38.         /// <param name="templatDir"></param>  
    39.         public void SetTemplateDirPath(string templatDir)  
    40.         {  
    41.             Init(templatDir);  
    42.         }  
    43.         /// <summary>  
    44.         /// 设置模板文件  
    45.         /// </summary>  
    46.         /// <param name="templateName"></param>  
    47.         public void SetTemplateFileName(string templateName)  
    48.         {  
    49.             _templateName = templateName;  
    50.         }  
    51.   
    52.   
    53.         /// <summary>  
    54.         ///     初始化NVelocity模块  
    55.         /// </summary>  
    56.         /// <param name="templatDir">模板文件夹路径</param>  
    57.         public void Init(string templatDir)  
    58.         {  
    59.             //创建VelocityEngine实例对象并设置初始化VelocityEngine   
    60.             _velocity = new VelocityEngine();  
    61.             _velocity.SetProperty(RuntimeConstants_Fields.RESOURCE_LOADER, "file");  
    62.             _velocity.SetProperty(RuntimeConstants_Fields.FILE_RESOURCE_LOADER_PATH, templatDir);  
    63.             _velocity.SetProperty(RuntimeConstants_Fields.INPUT_ENCODING, "utf-8");  
    64.             _velocity.SetProperty(RuntimeConstants_Fields.OUTPUT_ENCODING, "utf-8");  
    65.             _velocity.Init();  
    66.   
    67.             //为模板变量赋值  
    68.             _context = new VelocityContext();  
    69.         }  
    70.   
    71.         /// <summary>  
    72.         ///     给模板变量赋值  
    73.         /// </summary>  
    74.         /// <param name="key">模板变量</param>  
    75.         /// <param name="value">模板变量值</param>  
    76.         public void Put(string key, object value)  
    77.         {  
    78.             if (_context == null)  
    79.             {  
    80.                 _context = new VelocityContext();  
    81.             }  
    82.             _context.Put(key, value);  
    83.         }  
    84.   
    85.         /// <summary>  
    86.         ///     渲染模板  
    87.         /// </summary>   
    88.         public string Render()  
    89.         {  
    90.             if (!string.IsNullOrEmpty(_templateName))  
    91.             {  
    92.                 //从文件中读取模板  
    93.                 Template template = _velocity.GetTemplate(_templateName);  
    94.   
    95.                 //合并模板  
    96.                 var writer = new StringWriter();  
    97.                 template.Merge(_context, writer);  
    98.                 return writer.GetStringBuilder().ToString();  
    99.             }  
    100.             return "未指定模板文件!";  
    101.         }  
    102.     }  
    103. }  

    完整Demo下载:http://download.csdn.net/detail/a497785609/5405089

    相关资料:

    掌握一种 C#/.Net 模板技术 — Velocity:http://unmi.cc/csharp-velocity-templat

    Velocity初体验 http://tech.163.com/04/1230/03/18QP3L080009159J.html

    NVelocity介绍,NVelocity中文手册文档及实例下载 http://tommyhu.cn/NVelocity/ 

  • 相关阅读:
    簡化SQL Insert、Update、Delete、Select的方法
    Microsoft SqlHelper Class
    EntLib5.0 日志应用程序块(logging) 使用与配置
    EnterpriseLibrary及Log4Net於WebConfig或AppConfig的相關配置
    LogHelper
    log4net使用详解
    Create\Move\Delte Folder\File
    Log4Net使用详解(续)
    DataSet 类(四)读写XML
    SqlDataAdapter类
  • 原文地址:https://www.cnblogs.com/wangluochong/p/5981132.html
Copyright © 2020-2023  润新知