Velocity的特殊字符

Getting literal

Velocity 使用特殊字符$和#来帮助它工作,所以如果要在template 里使用这些特殊字符

要格外小心。本节将讨论$字符。

 

货币字符

在VTL 中使用$2.5 这样的货币标识是没有问题得的,VTL 不会将它错认为是一个

reference,因为VTL 中的reference 总是以一个大写或者小写的字母开始。

 

Escaping valid VTL reference

VTL 中使用“\”作为逃逸符。

例如:

#set( $email = “foo” )

$email

\$email

\\$email

\\\$email

将render 为:

foo

$email

\foo

\\$email

如果email 变量没有被定义则

$email

\$email

\\$email

\\\$email

将被render 为:

$email

\$email

\\$email

\\\$email

注意:VTL 中未被定义的变量将被认为是一个字符串,所以以下例子:

#set( $foo = “gibbous” )

$moon = $foo

的输出结果是:

$moon = gibbous

 

 

Case substitution

现在你已经对reference 比较熟悉了,你可以将他们高效的应用于你的template 了。

Velocity 利用了很多java 规范以方便了设计人员的使用。例如:

$foo

$foo.getBar()

## is the same as

$foo.Bar

$data.getUser(“jon”)

## is the same as

$data.User(“jon”)

$data.getRequest().getServerName()

# is the same as

$data.Request.ServerName

## is the same as

${data.Request.ServerName}

但是,注意VTL 中不会将reference 解释为对象的实例变量。例如:$foo.Name 将被解

释为Foo 对象的getName()方法,而不是Foo 对象的Name 实例变量。

 

 

 

Directives

Reference 允许设计者使用动态的内容,而directive 使得你可以应用java 代码来控制你

的显示逻辑,从而达到你所期望的显示效果。

 

#set

#set directive 被用于设置一个reference 的值。例如:

#set ( $primate = “monkey” )

#set ( $customer.Behavior = $primate )

赋值左侧的(LHS)必须是一个变量或者属性reference。右侧(RHS)可以是以下

类型中一种:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值