render
Purpose
Applies an inbuilt or user defined Groovy template against a model so that templates can be re-used for lists or instance or a single instanceExamples
Example domain class:class Book {
String title
String author
}
Example template:<p>${it.title}</p>
<p>${it.author}</p>
This template can now be re-used whether you have a list of books or a single book. For a list the template will be repeated for each instance:<g:render template="displaybook" collection="${books}" />
or<g:render template="displaybook" bean="${book}" />
or you could create a template that handles a certain type of model. For example the template below:<p><%= book.title %></p>
<p><%= author.fullName %></p>
Could be used with the model as below. The disadvantage of this technique however is that the template is less re-usable<g:render template="displaybook" model="['book':book,'author':author]" />
It is also possible to define the name of the variable to be used by the template in the render tag:Example template:<p>${myBook.title}</p>
<p>${myBook.author}</p>
Example render tag call for the above template<g:render template="displaybook" collection="${books}" var="myBook"/>
Description
Note that if the value of the template attribute starts with a '/' it will be resolved relative to the views folder. This is useful for sharing templates between views. Without the leading '/' it will be first be resolved relative to the current controller's view folder then, failing that, the top level views folder. In either case the template file must be named with a leading underscore ('_') but referenced in the template attribute without that underscore or the '.gsp' suffix.Attributes
template
(required) - The name of the template to apply
bean
(optional) - The bean to apply the template against
model
(optional) - The model to apply the template against as a java.util.Map
collection
(optional) - A collection of model objects to apply the template to
var
(optional) - The variable name of the bean to be referenced in the template
Source
Show Source
def render = { attrs, body ->
if(!groovyPagesTemplateEngine) throw new IllegalStateException("Property [groovyPagesTemplateEngine] must be set!")
if(!attrs.template)
throwTagError("Tag [render] is missing required attribute [template]") def engine = groovyPagesTemplateEngine
def uri = grailsAttributes.getTemplateUri(attrs.template,request)
def var = attrs['var']
def contextPath = attrs.contextPath ? attrs.contextPath : "" def r = engine.getResourceForUri("${contextPath}${uri}")
if(!r.exists()) r = engine.getResourceForUri("${contextPath}/grails-app/views/${uri}")
def t = engine.createTemplate( r ) if(attrs.model instanceof Map) {
t.make( attrs.model ).writeTo(out)
}
else if(attrs.containsKey('collection')) {
attrs.collection.each {
if(var) {
def b = [:]
b.put(var, it)
t.make(b).writeTo(out)
}
else {
t.make( ['it': it] ).writeTo(out)
}
}
}
else if(attrs.containsKey('bean')) {
if(var) {
def b = [:]
b.put(var, attrs.bean)
t.make(b).writeTo(out)
}
else {
t.make( [ 'it' : attrs.bean ] ).writeTo(out)
}
}
else if(attrs.template) {
t.make().writeTo(out)
}
}