---
title: "Grails：模板技术"
author: "fighterhou"
date: "2011-09-23T14:01:39Z"
tags: []
categories: ["互联网"]
slug: "grails"
draft: false
lastmod: "2011-09-23T14:01:39Z"
id: 1178966
url: "https://fighterhou.iteye.com/blog/1178966"
---

模板技术用于将不同页面间公共的显示内容独立封装起来。 模板页面是以"\_"开头的".gsp"文件，存放在views目录下（template属性以”/“开头）或者controller的views目录下（template属性不以"/"开头）。 在目标页面中，使用标签引用模板的内容。 当template中只使用一个数据对象时：

${it.title}

${it.author}

如果使用collecton属性传入集合，则会对集合中的每一个元素重复上述内容。 如果使用bean属性传入对象，则只会执行一遍： 如果不想使用默认的it作为数据对象的名称，则可以修改：

${myBook.title}

${myBook.author}

然后使用var属性为传入的数据改名： 当template中需要使用多个数据对象时：

&lt;%= book.title %&gt;

&lt;%= author.fullName %&gt;

则需要使用model属性传入数据：