velocity 模板语法

article/2025/9/14 2:39:55

velocity 模板语法

    • 前言
    • 1. 访问
      • 1.1 变量
      • 1.2 属性
      • 1.3 方法
    • 2. 指令
      • 2.1 #set 创建变量
      • 2.2 #if/#elseif/#else 分支控制
      • 2.3 #foreach 循环控制
      • 2.4 #include - 引入本地文件,文本展示
      • 2.5 #parse - 引入本地文件,velocity 解析后展示
      • 2.6 #stop - 停止模板解析
      • 2.7 #break - 停止当前指令
      • 2.8 #evaluate - 动态解析字符串或引用
      • 2.9 #define - 定义一个“块”引用
      • 2.10 #macro - 定义一个带参数的块
    • 3 注释
      • 3.1 单行注释
      • 3.2 多行注释
      • 3.3 带特殊字符注释

前言

翻译自: 官网 Apache Velocity Project

1. 访问

1.1 变量

$ [ ! ][ { ][a…z, A…Z ][ a…z, A…Z, 0…9, -, _ ][ } ]

示例:

$mud-Slinger_9
$!mud-Slinger_9
${mud-Slinger_9}
$!{mud-Slinger_9}

1.2 属性

Notation:

$ [ { ][ a…z, A…Z ][ a…z, A…Z, 0…9, -, _ ] .[a…z, A…Z ][a…z, A-Z, 0…9, -, _ ] [ } ]

示例:
$customer.Address
${purchase.Total}
对于 getProp(), isProp 可以用 $obj.prop 代替

1.3 方法

Notation:

$ [ { ][ a…z, A…Z ][ a…z, A…Z, 0…9, -, _ ] .[ a…z, A…Z ][a…z, A…Z, 0…9, -, _ ]( [optional parameter list…* ] ) [ } *]

示例:
$customer.getAddress()
${purchase.getTotal()}
$page.setTitle( “My Home Page” )

VTL Properties can be used as a shorthand notation for VTL Methods that take get and set. Either $object.getMethod() or $object.setMethod() can be abbreviated as $object.Method. It is generally preferable to use a Property when available. The main difference between Properties and Methods is that you can specify a parameter list to a Method.
对于 $object.getMethod(), $object.getMethod() 方法 可以使用 $object.method 代替

2. 指令

2.1 #set 创建变量

格式: # [ { ] set [ } ] ( $ref = [ ", ’ ]arg[ ", ’ ] )

参数说明:
$ref - The LHS of the assignment must be a variable reference or a property reference.
表达式左侧必须是一个变量引用或者属性引用
arg - The RHS of the assignment, arg is parsed if enclosed in double quotes, and not parsed if enclosed in single quotes. If the RHS evaluates to null, it is not assigned to the LHS.
表达式右侧,如果由双引号包裹,会被解析,单引号则不会,如果右侧表达式值为null,则不会被赋值

双引号单独说明一下
$className 为 “test0”
#{set}($test=“$className”)
$test // 输出为 test0

Examples:

Variable reference: #set( $monkey = $bill )
变量引用
String literal: #set( $monkey.Friend = ‘monica’ )
字符串字面量
Property reference: #set( $monkey.Blame = $whitehouse.Leak )
属性用用
Method reference: #set( $monkey.Plan = s p i n d o c t o r . w e a v e ( spindoctor.weave( spindoctor.weave(web) )
方法引用
Number literal: #set( $monkey.Number = 123 )
数字字面量
Range operator: #set( $monkey.Numbers = [1…3] )
数组
Object list: #set( $monkey.Say = [“Not”, $my, “fault”] )
对象列表
Object map: #set( $monkey.Map = {“banana” : “good”, “roast beef” : “bad”})
对象map

The RHS can also be a simple arithmetic expression, such as:
右侧表达式也可以是简单的数学表达式

Addition: #set( $value = $foo + 1 )

Subtraction: #set( $value = $bar - 1 )

Multiplication: #set( $value = $foo * $bar )

Division: #set( $value = $foo / $bar )
取余
Remainder: #set( $value = $foo % $bar )

2.2 #if/#elseif/#else 分支控制

Format:

# [ { ] if [ } ] ( [condition] ) [output] [# [ { ] elseif [ } ] ( [condition] ) [output] ]* [# [ { ] else [ } ] [output] ] # [ { ] end [ } ]

Usage:

condition - If a boolean, considered true if it has a true false; if not a boolean, considered true if not null.
output - May contain VTL.

在这里插入图片描述

The == operator can be used to compare numbers, strings, objects of the same class, or objects of different classes. In the last case (when objects are of different classes), the toString() method is called on each object and the resulting Strings are compared.
== 操作符可以用来比较 数字,字符串,相同类型对象,不同类型对象。如果是不同类型对象,会先调用 toString 方法,然后使用字符串比较

2.3 #foreach 循环控制

# [ { ] foreach [ } ] ($ref in arg) statement # [ { ] end [ } ]

  • $ref - The first variable reference is the item.
    列表中的元素
  • arg - May be one of the following: a reference to a list (i.e. object array, collection, or map), an array list, or the range operator.
    可以是 对象数组,集合,map, 数组列表,范围操作符
  • statement - What is output each time Velocity finds a valid item in the list denoted above as arg. This output is any valid VTL and is rendered each iteration of the loop.
    输出内容

Examples of the #foreach(), omitting the statement block :

Reference: #foreach ( $item in $items )
Array list: #foreach ( $item in [“Not”, $my, “fault”] )
Range operator: #foreach ( $item in [1…3] )

Additionally, the maximum allowed number of loop iterations can be controlled engine-wide (an ability introduced in Velocity 1.5). By default, there is no limit:
可以通过设置,指定 foreach list的最大长度
# The maximum allowed number of loops.
directive.foreach.maxloops = -1

2.4 #include - 引入本地文件,文本展示

Format:

# [ { ] include [ } ] ( arg[ arg2 … argn] )

Usage:

arg - Refers to a valid file under TEMPLATE_ROOT.
路径一定要是 模板根目录下的

Examples:

String: #include( “disclaimer.txt” “opinion.txt” )
字符串引用
Variable: #include( $foo $bar )
变量引用

2.5 #parse - 引入本地文件,velocity 解析后展示

Format:

# [ { ] parse [ } ] ( arg )

Usage:

arg - Refers to a template under TEMPLATE_ROOT.
Examples:

String: #parse( “lecorbusier.vm” )
Variable: #parse( $foo )
允许循环引用,默认最深10层。可以设置参数 parse_directive.maxdepth 决定最大深度
Recursion permitted. See parse_directive.maxdepth in velocity.properties to change from parse depth. (The default parse depth is 10.)

2.6 #stop - 停止模板解析

Format:

# [ { ] stop [ } ]

Usage:

This will stop execution of the current template. This is good for debugging a template.
停止解析模板,可以用来debug模板解析

2.7 #break - 停止当前指令

Format:

# [ { ] break [ } ]

Usage:

This will break execution of the current content directive. This is good for exiting a #foreach loop early, but also works in other scopes. You can even pass the scope control reference for a specific outer scope to break execution of all scopes outward to the specified one.
停止当前指令解析,也可以跳出当前 scope, 到外部 scope

2.8 #evaluate - 动态解析字符串或引用

Format:

# [ { ] evaluate [ } ] ( arg )

Usage:

arg - String literal or reference to be dynamically evaluated.
字符串或者字符串引用
Examples:

String: #evaluate( ‘string with VTL #if(true)will be displayed#end’ )
Variable: #evaluate( $foo )

2.9 #define - 定义一个“块”引用

Format:

# [ { ] define [ } ] ( $ref )statement# [ { ] end [ } ]

Usage:

$ref - Reference that is assigned the VTL block as a value.
标识 “块” 的名称
statement - Statement that is assigned to the reference.
“块” 的内容
Example:

#define( $hello ) Hello $who #end 
#set( $who = "World!") 
$hello   ## displays Hello World!

2.10 #macro - 定义一个带参数的块

Format:

# [ { ] macro [ } ] ( vmname $arg1 [ $arg2 $arg3 … $argn ] ) [ VM VTL code… ] # [ { ] end [ } ]

Usage:

  • vmname - Name used to call the VM (#vmname)
    块名称
  • 参数列表
    $arg1 $arg2 [ … ] - Arguments to the VM. There can be any number of arguments, but the number used at invocation must match the number specified in the definition.
    可以有任意数量的参数,调用的参数数量必须与定义一直
  • [ VM VTL code… ] - Any valid VTL code, anything you can put into a template, can be put into a VM.
    Once defined, the VM is used like any other VTL directive in a template.
    块的内容
  • 调用
#vmname( $arg1 $arg2 )

Except, that when you wish to call a VM with a body, then you must prefix the name of the VM with @. The content of that body may be referenced in the macro definition via ! b o d y C o n t e n t a s m a n y o r f e w t i m e s a s y o u l i k e . 特 别 的 , 当 你 想 使 用 带 b o d y 的 参 数 调 用 时 , 必 须 要 在 块 名 称 前 加 上 “ @ ” 。 b o d y 内 容 可 以 在 块 中 使 用 , 通 过 ‘ !bodyContent as many or few times as you like. 特别的,当你想使用 带 body的参数调用时,必须要在块名称前加上“@”。body 内容可以在块中使用,通过 ` !bodyContentasmanyorfewtimesasyoulike.使body@body使!bodyContent` 使用

#@vmname( $arg1 $arg2 ) here is the body#end

VMs can be defined in one of two places:
带参数块可以定义在两个地方

  • Template library: can be either VMs pre-packaged with Velocity or custom-made, user-defined, site-specific VMs; available from any template
  • Inline: found in regular templates, only usable when velocimacro.permissions.allowInline=true in velocity.properties.
    设置 velocimacro.permissions.allowInline=true 时,可以在行内使用

3 注释

3.1 单行注释

## This is a comment.**

3.2 多行注释

#**This is a multiline comment.This is the second line.
*#

3.3 带特殊字符注释

#[[This has invalid syntax that would normally need 
"poor man's escaping" like:- #define()- ${blah]]#

http://chatgpt.dhexx.cn/article/KdIoWSOI.shtml

相关文章

finalize() 原理

finalize 方法的作用是: 如果对象在进行可达性分析后发现没有与 GC Roots 相连接的引用链,那他将会被第一次标记并且进行一次筛选,筛选的条件是此对象是否有必要执行 finalize 方法。 注意:当对象没有覆盖 finalize 方法&#xf…

finalize()

注:本文的目的并不是鼓励使用finalize方法,而是大致理清其作用、问题以及GC执行finalize的过程。 1. finalize的作用 finalize()是Object的protected方法,子类可以覆盖该方法以实现资源清理工作,GC在回收对象之前调用该方法。fina…

java finalize方法详解

1. finalize的作用 finalize()是Object的protected方法,子类可以覆盖该方法以实现资源清理工作,GC在回收对象之前调用该方法。finalize()与C中的析构函数不是对应的。C中的析构函数调用的时机是确定的(对象离开作用域或delete掉)&…

java中finalize()方法

finalize 垃圾回收机器(Garbage Collection),也叫GC,垃圾回收器主要有一下特点: 当对象不再被程序所使用的时候,垃圾回收器将会将其回收垃圾回收是在后台运行的,我们无法命令垃圾回收器马上回收资源&…

Finalize详解

finalize()方法详解,前言,finalize()是Object的protected方法,子类可以覆盖该方法以实现资源清理工作,GC在回收对象之前调用该方法。 finalize的作用: (1)finalize()与C中的析构函数不是对应的。C中的析构函数调用的时机是确定的…

finalize的理解

finalize的理解 一般的回答:它是Object中的一个方法,子类重写它,垃圾回收时候方法会被调用,可以再其中进行一些资源的解释和清理工作。 优秀的回答:将资源的释放和清理放在finalize方法中是非常不好的,影…

Java FX swt_DOC-13-08 JavaFX与SWT的协作性

DOC-13-08 JavaFX与SWT的协作性 本章展示了如何在SWT应用程序中加入一个JavaFX场景图,以及如何使SWT和JavaFX控件协作。 介绍 如果你开发SWT应用程序,你知道SWT使用本地操作系统的控件,而且并不能简单的配置来使用高级GUI特性,比如…

Java SWT 表格Table如何动态显示信息

让Table显示信息用到的是TableItem类。创建一个TableItem类对象,通过调用该对象的setText( new String[ ] )方法可以显示一行数据,循环调用则可以显示多条不同的数据。 一、步骤: 1. 创建Table类 。最好将Table类设置为全局变量。并且设置该表格有多少列…

Java ——SWT利用DateTime获取日历控件

1、话不多说,先看效果图: 19是我选择的日期,其他功能就不一一介绍了,这个看你们自己。 2、Test01界面: Test01代码如下: package test; import org.eclipse.swt.widgets.Display; import org.eclipse.swt…

shell swt 样式_SWT之路:SWT图像显示

简明现代魔法 -> Java编程语言 -> SWT之路:SWT图像显示 SWT之路:SWT图像显示 2009-10-03 程序演示 还是先用SWT Desiner创建界面程序。然后创建一个Display对象和Image对象,和一个GC对象。类org.eclipse.swt.graphics.GC是一个封装了所…

eclipse java swt_Eclipse下搭建SWT开发环境

0.序言 还是老风格,从头写些基本的东西,帮助自己,也帮助正处于困惑中的别人。今天介绍的是Eclipse下的SWT的配置过程。自己前两天要做个项目,配置了半天都不正确,后来慢慢总结了一下,不同环境配置的方法可能…

java swt button_JAVA.SWT/JFace: SWT基本组件之按钮(Button)

《Eclipse SWT/JFACE 核心应用》 清华大学出版社 5.2 按钮(Button) 按钮有普通按钮(SWT.PUSH)、单选按钮(SWT.RADIO)、多选按钮(SWT.CHECK)、箭头按钮(SWT.ARROW)和切换按钮(SWT.TOGGLE)几种类型。 同时,也可以设置按钮的样式。设置按钮文字对齐的样式有SWT.LEFT、S…

Java SWT 表格Table实时刷新数据

一、动态展示数据 当对表格展示的数据进行删除和增加的时候,想实时进行表格数据更新显示。用到的方法是,首先将表格数据全部删掉,然后在读取数据库最新的数据显示到表格中。  可以将显示表格信息的代码封装成一个方法,当对当前表…

Java SWT封装Table显示数据

一个表格就类似于一张二维表。第一行为关系模型,就是每一列的列名。从第二行开始就是表的数据,简称元组。下面实现对其Table的封装。 首先显示一个窗口。创建一个Table。将在showTableInfos()方法对Table表格进行封装。 import org.eclipse.swt.widgets.…

SWT控件总结

控件palette 1.System Choose component 选择组件:允许选择组件类型并将其拖放到设计画布上 Tab Order标签的顺序:设置所选选项卡顺序 2.Composites Composite 复合:能够包含其他控件的控件 Group 集团:提供带有可选标题的蚀…

SWT和JFace应用笔记

SWT和JFace应用笔记 链接:https://pan.baidu.com/s/1To4Lhgan4xEr1iaFA0Rerg 提取码:63qm 1.SWT笔记 一.创建一个SWT程序有3个部分 1.初始化窗口:首先创建Dispaly对象和Shell对象。Display:对象封装了调用操作系统的有关方法Shell&#xf…

matlab swt函数,matlab swt 函数出错

matlab swt 函数出错 我在用matlab swt 函数分解信号时总是出现以下错误,麻烦各位高手告知该怎么修改,swt函数如何ERROR ... ---------------------------------------------------------------------------------------------------------------------…

【SWT】自定义数据表格

目的 使用SWT技术自定义数据表格,本文抛砖引玉,给出了SWT构建数据表格的基本思路和简单实现。更多特殊需求即表格功能实现待续…… 思路 数据表格由表格头与表格体两边部分组成。 表格头部分是固定的,其字段右侧包含一个简单的表格工具–…

【SWT】Lable 文字折行

目标 当Label 中的文字过多时,使得文字折行显示。 效果如图所示: 分析与实践 Label 自带样式是一行显示所有信息。当一行显示不下时,超出部分会被隐藏掉,当Label有足够长度时再将其展示出来。Label这种处理超出部分的方式很粗…

java swt 几种布局_实战SWT布局

fortune 阅读(577) 评论(0) 编辑 收藏 所属分类: java技术 标准的SWT布局类FillLayout:在容器中以相同的大小单行或单列的排列组件 RowLayout:以单行或多行的方式使用几个选项(fill,wrap,spacing,justify,type)定制组件的排列方式 GridLayout&#xff…