强大的模板引擎开源软件NVelocity

强大的模板引擎开源软件NVelocity

背景知识
NVelocity(http://sourceforge.net/projects/nvelocity )是从java编写的Velocity移植的.net版本,是java界超强的模版系统,.net版本的NVelocity目前版本为0.42。
NVelocity拥有完善的、丰富的模板语言(VTL,Velocity Template Language) ,几乎所有高级语言的特性都可以在模板引擎语言中找到。(如流程控制语句、数学运算、关系和逻辑操作符、函数定义、注释等等)

NVelocity可以做什么?
能够快速生成所需的代码、SQL脚本、页面文件等基于文本内容的文件
生成速度快、模板语言完善、灵活性好
容易学习、开源,免费使用
前身为Velocity(Java),使用者多,资料全

用途
在编写代码的时候,我们可以发现很多内容都是不需要变化的,变化的只是一小部分内容,对不同的对象,这些内容不同。
如果我们需要生成一个变化的文档,是否需要在代码拷贝这些不变化的内容么,或者把它剥离开放到其他的文件去么?还有我们是否能对这些内容有一些简单的控制和引用么?

简单例子(主要规则:引用以$开头用于取得什么东西,而指令以# 开始用于做什么事情)

#set($foo = false)
#if ($foo)

    this is true
#elseif ($bar)
    this is false
#elseif (true)
    this should be followed by two blank lines
#end

## this is a single line comment

#*

this is a multi line comment
#if (
*#


#set($user = "jason")
#set($login = false)
#set($count = 5)

#if ($user == "jason")

    the user $user is logged in!
#end

#if ($count == 5)

    the count is 5!
#end

#if ($login == false)

    the user isn't logged in.
#end

#if ($count != 3)
    \$count is not equal to 3
#end



变量说明
在VTL中,所有变量标识符的开头要加上$字符,如$Name,也可以用一种更加明确的方法表示,例如${name}。
变量标识符被映射到稍后即将介绍的VelocityContext对象。在模板引擎处理模板时,变量名称(如name)被替换成VelocityContext中提供的值

C#代码

 

Velocity.Init("nvelocity.properties");

VelocityContext context 
= new VelocityContext();
context.Put(
"list", Names);

Template template 
= null;
try
{
   template 
= Velocity.GetTemplate("test.cs.vm");
}

catch (ParseErrorException pee)
{
   System.Console.Out.WriteLine(
"Syntax error: " +  pee);
}

if (template != null)
{
   template.Merge(context, System.Console.Out);
}


 

注释
单行注释
## This is a single line comment


多行注释
#*
 Thus begins a multi-line comment. Online visitors won't
 see this text because the Velocity Templating Engine will
 ignore it.
*#


属性或方法
$customer.Address
$purchase.Total

$page.SetTitle( "My Home Page" )
$person.SetAttributes( ["Strange", "Weird", "Excited"] )


指令
#set( $primate = "monkey" )
#set( $monkey.Friend = "monica" )

#set( $criteria = ["name", "address"] )
#foreach( $criterion in $criteria )

    #set( $result = $query.criteria($criterion) )
    #if( $result )
        Query was successful
    #end
#end


If / ElseIf / Else
Foreach 循环

#if( $foo < 10 )
    <strong>Go North</strong>
#elseif( $foo == 10 )
    <strong>Go East</strong>
#elseif( $bar == 6 )
    <strong>Go South</strong>
#else
    <strong>Go West</strong>
#end        


<ul>
#foreach( $product in $allProducts )
    <li>$product</li>
#end
</ul>

宏 (称为函数更合适)
#macro 脚本元素允许模板设计者在VTL 模板中定义重复的段。 Velocimacros 不管是在复杂还是简单的场合都非常有用。下面这个Velocimacro,仅用来节省击键和减少排版错误,介绍了一些NVelocity宏的概念。
可以带参数,参数放在宏名称的后面,空格隔开

#macro( d )
<tr><td></td></tr>
#end

#d()


#macro( callme $a )
         $a $a $a
#end
 
#callme( $foo.bar() )

包含
#include 脚本元素允许模板设计人员包含(导入)本地文件, 这个文件将插入到#include 指令被定义的地方。文件的内容并不通过模板引擎来渲染。
#include( "one.txt" )

解析
#parse 脚本元素允许页面设计员导入包含VTL的本地文件。 Velocity将解析和渲染指定的模板。
#parse( "me.vm" )


在根目录NVelocity-***\test\templates下有各种模板语言语法的实例,在NVelocity-***\ examples目录下有如何在C#中使用模板引擎的简单例子。
在.NET中使用时候,需要应用两个程序集,NVelocity.dll 和 Commons.dll,这些文件在NVelocity-***\Build目录下。
可以加入nvelocity.properties对模板引擎的一些参数进行配置。

posted on 2007-11-17 00:01  伍华聪  阅读(19036)  评论(30编辑  收藏  举报

导航