Velocity简单语法及VelocityHelper封装

时间:2022-11-25 00:00:24
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

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

namespace NVelocity
{
/// <summary>
/// NVelocity模板工具类 VelocityHelper
/// </summary>
public class VelocityHelper
{
private IContext _context;
private VelocityEngine _velocity;
private string _templateName;

/// <summary>
/// 构造函数
/// </summary>
/// <param name="templatDir">模板文件夹路径</param>
/// <param name="templateName">模板文件名</param>
public VelocityHelper(string templatDir, string templateName)
{
Init(templatDir);
_templateName = templateName;
}

/// <summary>
/// 无参数构造函数
/// </summary>
public VelocityHelper()
{
}

/// <summary>
/// 设置模板文件夹
/// </summary>
/// <param name="templatDir"></param>
public void SetTemplateDirPath(string templatDir)
{
Init(templatDir);
}
/// <summary>
/// 设置模板文件
/// </summary>
/// <param name="templateName"></param>
public void SetTemplateFileName(string templateName)
{
_templateName = templateName;
}


/// <summary>
/// 初始化NVelocity模块
/// </summary>
/// <param name="templatDir">模板文件夹路径</param>
public void Init(string templatDir)
{
//创建VelocityEngine实例对象并设置初始化VelocityEngine
_velocity = new VelocityEngine();
_velocity.SetProperty(RuntimeConstants_Fields.RESOURCE_LOADER, "file");
_velocity.SetProperty(RuntimeConstants_Fields.FILE_RESOURCE_LOADER_PATH, templatDir);
_velocity.SetProperty(RuntimeConstants_Fields.INPUT_ENCODING, "utf-8");
_velocity.SetProperty(RuntimeConstants_Fields.OUTPUT_ENCODING, "utf-8");
_velocity.Init();

//为模板变量赋值
_context = new VelocityContext();
}

/// <summary>
/// 给模板变量赋值
/// </summary>
/// <param name="key">模板变量</param>
/// <param name="value">模板变量值</param>
public void Put(string key, object value)
{
if (_context == null)
{
_context = new VelocityContext();
}
_context.Put(key, value);
}

/// <summary>
/// 渲染模板
/// </summary>
public string Render()
{
if (!string.IsNullOrEmpty(_templateName))
{
//从文件中读取模板
Template template = _velocity.GetTemplate(_templateName);

//合并模板
var writer = new StringWriter();
template.Merge(_context, writer);
return writer.GetStringBuilder().ToString();
}
return "未指定模板文件!";
}
}
}
完整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/