applyLayout
Purpose
Applies the specified layout to either the body, a given template and an arbitrary URL allowing the development of "portlet" style applications and mashups
Examples
<g:applyLayout name="myLayout" template="displaybook" collection="${books}" />
or<g:applyLayout name="myLayout" url="http://www.google.com" />
or<g:applyLayout name="myLayout">
The content to apply a layout to
</g:applyLayout>
Description
Attributes
name
- The name of the layout to apply
url
- The URL of the content to apply, could be an external URL
template
(optional) - 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
Source
Show Source
def applyLayout = { attrs, body ->
if(!groovyPagesTemplateEngine) throw new IllegalStateException("Property [groovyPagesTemplateEngine] must be set!")
def oldPage = getPage()
def contentType = attrs.contentType ? attrs.contentType : "text/html" def content = ""
GSPSitemeshPage gspSiteMeshPage = null
if(attrs.url) {
content = new URL(attrs.url).text
} else {
def oldGspSiteMeshPage=request.getAttribute(GrailsPageFilter.GSP_SITEMESH_PAGE)
try {
gspSiteMeshPage = new GSPSitemeshPage()
request.setAttribute(GrailsPageFilter.GSP_SITEMESH_PAGE, gspSiteMeshPage)
if(attrs.view || attrs.template) {
content = render(attrs)
}
else {
def bodyClosure = GroovyPage.createOutputCapturingClosure(this, body, webRequest, true)
content = bodyClosure()
}
if(content instanceof StreamCharBuffer) {
gspSiteMeshPage.setPageBuffer(content)
} else if (content != null) {
def buf=new StreamCharBuffer()
buf.writer.write(content)
gspSiteMeshPage.setPageBuffer(buf)
}
} finally {
request.setAttribute(GrailsPageFilter.GSP_SITEMESH_PAGE, oldGspSiteMeshPage)
}
} def page = null
if(gspSiteMeshPage != null && gspSiteMeshPage.isUsed()) {
page = gspSiteMeshPage
} else {
def parser = getFactory().getPageParser(contentType)
page = parser.parse(content.toCharArray())
} attrs.params?.each { k,v->
page.addProperty(k,v?.toString())
}
def decoratorMapper = getFactory().getDecoratorMapper() if(decoratorMapper) {
def d = decoratorMapper.getNamedDecorator(request, attrs.name)
if(d && d.page) {
try {
request[PAGE] = page
def t = groovyPagesTemplateEngine.createTemplate(d.getPage())
def w = t.make()
w.writeTo(out) } finally {
request[PAGE] = oldPage
}
}
}
} private Factory getFactory() {
return FactoryHolder.getFactory()
}