Skip to main content.

GroovyMag blog

Blog for Groovy Magazine

I'm planning to do this on a couple other projects, and thought I'd share this with you. It's not specifically something related to GroovyMag magazine, but is a technique I'm using in a Grails project to make it easy to switch layouts based on the user.

All the default generated views use the meta tag 'layout' to point to a GSP file which will get merged with your view file (using sitemesh) to generate the final rendered HTML. Unfortunately these are always hardcoded to 'main' rather than a configurable option. So, I've taken to modifying the templates used during generation to point to a configuration value.

Run "grails install-templates" then look in src/templates/scaffolding. You'll see the create/edit/list/show GSP files which are used as the basis for generation.

Edit the files such that the meta line

<meta name="layout" content="main" />

looks like this

<meta name="layout" content="\${session?.layout?:grailsApplication.config.sitemesh.layout}" />

Then edit your grails-app/conf/Config.groovy file to have a line that reads

sitemesh.layout = "main"

If you then generate views for an app, everything will continue to work as it normally does by default; specifically, all the views will use the grails-app/views/layouts/main.gsp as a sitemesh wrapper for rendering.

However, you're now able to set the layout to something different for each user by setting the session.layout value.

If a user has authenticated and is an administrator, perhaps you'd want to have

session.layout = "admin"

in a controller action, which would use the grails-app/views/layouts/admin.gsp file for rendering views for an admin.

If views for entire controllers require a different view, you should be able to set the global config value for all users by doing something like

grailsApplication.config.sitemesh.layout = "layout2"

in a controller action.

GroovyMag is running a survey to help learn more about you, the readers. Take the survey and enter to win the upcoming December 2008 issue! The survey shouldn't take more than a couple minutes to fill out, and your feedback and input will help us make decision about what to focus on and how to improve the magazine for everyone.

We'll draw five winners out of the survey respondents after November 28. Winners will be notified by email ( you need to enter your email address at the bottom of the survey to enter ).

Take the survey today!

Many of you have probably already read about the announcement this morning from both SpringSource and G2One (Guillaume and Graeme go in to more detail on their respective blogs). What does this mean for Groovy and Grails?

In the short term, probably not actually all that much will change. Neither Graeme nor Guillaume have mentioned any timetable changes for the next Groovy and Grails releases. I suspect that combining resources with SpringSource may help with bug fixing and integration efforts in the immediate future.

Over the slightly longer term, this looks like it'll bolster the training and consulting offerings from SpringSource to address whatever dynamic language needs their customers had. G2One's existing customers will now have access to Spring experts.

The impact over the long haul is still an unknown, though it sounds like all parties involved have many ideas about what they'll be able to accomplish. We'll just need to wait and see what the future holds for Groovy and Grails.

I had a few extra minutes today to play with iMovie, and put together a little promotional video for GroovyMag. Take a look. :)

The GroovyMag "Spread The Wealth" sale, celebrating the US election (celebrating that it's finally over!), concluded today.

For 24 hours we offered GroovyMag at the bargain-basement price of $3.49, in honor of the 349 electoral votes received by the winning presidential candidate, Barack Obama. We @ GroovyMag had rather hoped Obama might be 402 electoral votes to ease the strain on the corporate coffers, but it is what it is.

Thank you to everyone who has purchased GroovyMag so far! Please send your feedback to editor@groovymag.com, or simply post a comment here in the blog. We'd love to know what you like, what you don't like, and what you're looking to see in future issues!