---
title: "单引号和双引号的小区别"
author: "xulongfa"
date: "2009-07-10T17:01:25Z"
tags: ["velocity"]
slug: "untitled"
draft: false
lastmod: "2009-07-10T17:03:25Z"
id: 423434
url: "https://xulongfa.iteye.com/blog/423434"
---

VTL语句以#开头，并包含指令(set)。变量以$开头，用引号引起。引号可以是单引号，也可以是双引号。 前者引用具体的String值；后者可以包含Velocity引用，例如”hello, $name”， $name会用其当前的值替换。上面的例子是将值Velocity赋值给变量a。

当使用#set 指令时，括在双引号中的字面字符串将解析和重新解释，如下所示：

```
#set( $directoryRoot = "www" )

#set( $templateName = "index.vm" )

#set( $template = "$directoryRoot/$templateName" )

$template
```

 

输出将会是：

www/index.vm

然而，当字面字符串括在单引号中时，他将不被解析：

```
#set( $foo = "bar" )

$foo

#set( $blargh = '$foo' )

$blargh
```

 

输出是：

Bar

$foo

默认情况下，使用单引号来渲染未解析文本在Velocity是有效的。这种特征可以通过编辑velocity.properties 中的 stringliterals.interpolate=false来改变。