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" params="[books: 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 
template - (optional) The template to apply the layout to 
url - (optional) The URL to retrieve the content from and apply a layout to 
contentType (optional) - The content type to use, default is "text/html" 
encoding (optional) - The encoding to use 
params (optional) - The params to pass onto the page object (retrievable with the pageProperty tag) 
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()
    }