Grails: Set sitemesh layout depending on request parameter

For Fancyboxes/Lightboxes you may want to reuse your existing views with just another layout. In the Reference Documentation you’ll find some methods to trigger the layout in a static manner.
If you want to do this dependening on a request parameter (for example layout=embedded) you have two possibilities:

1. Use a expression in the meta tag

    <meta name="layout" content="${params.layout ?: 'main'}">

With this approach you have to use the meta tag in your views and use the expression in every view where you want the layout to be changeable.

2. Define a filter

import org.codehaus.groovy.grails.web.sitemesh.GroovyPageLayoutFinder

 * Set layout depending on request parameter. This layout has the highest priority.
class LayoutFilters {
    def filters = {
        all(controller:'*', action:'*') {
            before = {
				if (params.layout) {
					request[GroovyPageLayoutFinder.LAYOUT_ATTRIBUTE] = params.layout

With this filter you have the ability to change the layout for every view in your application. If you only want this mechanism for some controller/actions, change the filter expression (see Applying Filters).


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: