48
技術社區[雲棲]
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
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下載:https://download.csdn.net/detail/a497785609/5405089
相關資料:
掌握一種 C#/.Net 模板技術 — Velocity:https://unmi.cc/csharp-velocity-templat
Velocity初體驗 https://tech.163.com/04/1230/03/18QP3L080009159J.html
NVelocity介紹,NVelocity中文手冊文檔及實例下載 https://tommyhu.cn/NVelocity/
最後更新:2017-04-03 18:51:58