6.2.3 视图和模板 - Reference Documentation
Authors: Graeme Rocher, Peter Ledbrook, Marc Palmer, Jeff Brown, Luke Daley, Burt Beckwith
Version: null
6.2.3 视图和模板
Grails also has the concept of templates. These are useful for partitioning your views into maintainable chunks, and combined with Layouts provide a highly re-usable mechanism for structured views.Use the render tag to render this template from one of the views in Notice how we pass into a model to use using the
Grails也有模板的概念。这对于将你的视图分割成可维护的模块也是颇有裨益的,并且结合布局还可为结构化视图提供一个高复用机制。Template Basics
Grails uses the convention of placing an underscore before the name of a view to identify it as a template. For example, you might have a template that renders Books located atgrails-app/views/book/_bookTemplate.gsp:<div class="book" id="${book?.id}"> <div>Title: ${book?.title}</div> <div>Author: ${book?.author?.name}</div> </div>
grails-app/views/book:<g:render template="bookTemplate" model="[book: myBook]" />model attribute of the render tag. If you have multiple Book instances you can also render the template for each Book using the render tag with a collection attribute:<g:render template="bookTemplate" var="book" collection="${bookList}" />模板基础
Grails使用在其视图名称前放置一个下划线的方式来标识一个模板。比如,你可能有一个渲染Books的模板,位于grails-app/views/book/_bookTemplate.gsp:<div class="book" id="${book?.id}"> <div>Title: ${book?.title}</div> <div>Author: ${book?.author?.name}</div> </div>
grails-app/views/book中的一个视图中,使用render标签来渲染此模板:<g:render template="bookTemplate" model="[book: myBook]" />render标签的model属性来传递模型的。如果你有多个Book实例,你还可以通过render标签的collection属性来为每一个Book渲染模板:<g:render template="bookTemplate" var="book" collection="${bookList}" />Shared Templates
In the previous example we had a template that was specific to theBookController and its views at grails-app/views/book. However, you may want to share templates across your application.In this case you can place them in the root views directory at grails-app/views or any subdirectory below that location, and then with the template attribute use an absolute location starting with / instead of a relative location. For example if you had a template called grails-app/views/shared/_mySharedTemplate.gsp, you would reference it as:<g:render template="/shared/mySharedTemplate" /><g:render template="/book/bookTemplate" model="[book: myBook]" />共享的模板
在上一个示例中,我们有了一个跟BookController相关的模板,其视图都位于grails-app/views/book中。然而,有时候,你可能想将你的模板在整个应用中共享。在这种情况下,你可以将模板放在grails-app/views这个视图根目录下,或者跟目录下的任意子目录中,然后在template属性中使用以/开头的绝对位置而非相对位置。比如,你有一个grails-app/views/shared/_mySharedTemplate.gsp模板,你就可以这样引用:<g:render template="/shared/mySharedTemplate" /><g:render template="/book/bookTemplate" model="[book: myBook]" />The Template Namespace
Since templates are used so frequently there is template namespace, calledtmpl, available that makes using templates easier. Consider for example the following usage pattern:<g:render template="bookTemplate" model="[book:myBook]" />tmpl namespace as follows:<tmpl:bookTemplate book="${myBook}" />模板的命名空间
因为模板是如此频繁地被使用,因此tmpl这个模板命名空间就产生了,这样模板的使用也更简易。比如下例所示地用法:<g:render template="bookTemplate" model="[book:myBook]" />tmpl命名空间的表达如下所示:<tmpl:bookTemplate book="${myBook}" />Templates in Controllers and Tag Libraries
You can also render templates from controllers using the render controller method. This is useful for Ajax applications where you generate small HTML or data responses to partially update the current page instead of performing new request:def bookData() {
def b = Book.get(params.id)
render(template:"bookTemplate", model:[book:b])
}def bookData() {
def b = Book.get(params.id)
String content = g.render(template:"bookTemplate", model:[book:b])
render content
}g namespace which tells Grails we want to use the tag as method call instead of the render method.
控制器和标签库的模板
你还可以在控制器中使用render方法来渲染模板。这在Ajax的应用中是非常有用的,你可以通过生成小的HTML或者数据响应来部分的更新当前页面,而不是发起一个新的请求:def bookData() {
def b = Book.get(params.id)
render(template:"bookTemplate", model:[book:b])
}def bookData() {
def b = Book.get(params.id)
String content = g.render(template:"bookTemplate", model:[book:b])
render content
}g命名空间的用法,它会让Grails知道我们想用标签的方法调用,而不是render方法。

