Are there any good tutorials for using sitemesh in a grails application?
Asked Answered
D

4

40

I'm a pretty experienced Grails developer, but most of my experience has been with using grails for serving up JSON/XML to a flex app and some relatively simple HTML websites.

I've been diving deeper into using the sitemesh integration in grails and I'm struggling a little to find best practices for some more complex configurations, and I'm curious if there are any good tutorials or examples out there. The original Sitemesh website isn't that useful as the tags it talks about aren't directly exposed in grails.

A google search is mostly showing old mailing list posts and some vanilla sitemesh stuff which is helping me to move a little further along, but it's a lot of trial and error.

I fully understand how the basic g:layoutTitle, g:layoutHead, and g:layoutBody tags work. Those are easy and well documented.

The kinds of things that I'd like to see examples for:

  • g:applyLayout - the documentation on this is weak and I don't fully understand the uses suggested in the main docs. How is this different than setting the meta name='layout' content='foo' property?

  • g:pageProperty - some better examples on how to pull and use properties into the main template by setting the values as meta tags in the page that's being decorated. The grails docs on pageProperty show only the onload attribute from the body being brought forward. I think you can also use meta tag values here as well, anything else?

  • can you use multiple levels of sitemesh layouts? My testing seems to make me think that I can't, but that seems to reduce reusability. I think that the answer here is some usage of the g:applyLayout, but that's where I'm struggling the most.

Digressive answered 4/12, 2008 at 2:19 Comment(0)
M
14

Well, I can answer a bit:

Your first and third questions are related, as you can't chain layouts using the meta tag.

Your final page should have a meta tag as you suggest, but if you want to layer a layout on top of another layout, you put a g:applyLayout tag at the top of the child layout, pointing at the parent.

In your edit.gsp, you'd have:

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

and in editTemplate.gsp, you'd have:

<g:applyLayout name="baseTemplate" >
<!-- the html for the editTemplate -->
</g:applyLayout>

so edit.gsp would use editTemplate.gsp, which would use baseTemplate.gsp as a base layout. You can chain those as needed.

I haven't used g:pageProperty at all, so I can't throw you better examples there, sorry.

Malposition answered 5/12, 2008 at 6:10 Comment(1)
This should get you going example wise Grails Goodness - Applying layouts in layoutsFukuoka
F
27

the g:pageProperty is a very powerful, but very poorly documented thing. Lets say in my layout I specify where to put some content like this:

<html>
<body>
<g:pageProperty name="page.header" />
</body>

Now in my page I can specify some content:

<content tag="header">
<!-- header -->
</content>

Sitemesh will take the content tag, regardless of actual position in the HTML of the page and place it where it needs to go in the flow of the layout.

Even better, if within my page I render a template that also specifies a content area with a tag of "header", it will overwrite the first declaration, and it will be the template's content that will be rendered in the final layout.

Fleer answered 28/5, 2009 at 15:6 Comment(2)
my god this is the exact answer to my issue, the point here is the use of 'page.' In the pageProperty name. +1000Sixfold
I've never seen this documented anywhere. I choose grails since lots of sites state that it's well documented... Not my experience so far..Pantsuit
M
14

Well, I can answer a bit:

Your first and third questions are related, as you can't chain layouts using the meta tag.

Your final page should have a meta tag as you suggest, but if you want to layer a layout on top of another layout, you put a g:applyLayout tag at the top of the child layout, pointing at the parent.

In your edit.gsp, you'd have:

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

and in editTemplate.gsp, you'd have:

<g:applyLayout name="baseTemplate" >
<!-- the html for the editTemplate -->
</g:applyLayout>

so edit.gsp would use editTemplate.gsp, which would use baseTemplate.gsp as a base layout. You can chain those as needed.

I haven't used g:pageProperty at all, so I can't throw you better examples there, sorry.

Malposition answered 5/12, 2008 at 6:10 Comment(1)
This should get you going example wise Grails Goodness - Applying layouts in layoutsFukuoka
F
4

The Sitemesh together with Grails is a very very powerful feature. The more I use it - the more I love it. You can decorate any part of our web site: you can have layout for error messages, tooltips, news lines, comments, etc, etc. Just to note that you can do even that with in your pages and have multiple levels of decoration (no <content> needed):

/view/layout/inline-error-message.gsp

<span class="errorMessageInSomeFancyBox">
    <span class="errorIcon"></span>
    <g:layoutBody />
<span>

/views/book/create.gsp

<%-- let's decorate our error message with some fancy box --%>
<g:applyLayout name="inline-error-message">${some.error.message}</g:applyLayout>
Falstaffian answered 28/2, 2011 at 17:44 Comment(2)
It's the same as templates with g:render. Is there any difference?Addax
This implementation is much cleaner if you have literally hundreds of templates. You don't want to end up with hundreds of g:renders and have many many properties in "${quotes}". This is easier to read and maintain. For small web site it does not make a difference.Falstaffian
M
0

See our Rabbtor Showcase App for a few very good examples on

  • creating nested layouts
  • rendering templates
  • applying layouts to specific parts of a page

. This app is actually a showcase for our tool Rabbtor which enables using GSP outside Grails but parts related with Sitmesh are also valid for Grails.

Maidstone answered 15/6, 2016 at 14:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.