views:

371

answers:

3

So it looks like Grails' GSP does not have an include tag (correct me if I'm wrong). The closest thing to "include" is to use the render tag. There is also the include plugin.

In my case, I have a classic top-left-middle-right-bottom paned page. The top header or left side would include navigation. The middle would be the content. The right side might include ads. In the JSP version of my app, each pane except for the middle would be in their own JSP. I'd then include (using <jsp:include/>) the various panels into my page. Is this the right approach in GSP?

What is the preferred pattern of accomplishing this in GSP?

A: 

You are correct that the render tag can accomplish what you want to do. Try it with templates, the key is to name them with an initial underscore. This blog post should be helpful.

Ed.T
+3  A: 

Instead of importing or rendering your panes per-view, you probably want to use the layout functionality that grails provides (with SiteMesh). If you want to apply the same layout to all of your pages (i.e. menu/content/ads), you can just modify grails-app/views/layouts/main.gsp to have the page elements you need and then the <g:layoutBody/> tag will be filled in by your regular views rendered by your controllers. The main.gsp layout is provided by default and is the default layout for all (scaffolded and generated) views.

If you need some pages to have the ads and others to not have them, you'll want to create a new layout in grails-app/views/layouts (e.g. main-ads.gsp). Then, in the views that you wish to have the ads, in the <head></head>, you'll set the layout using <meta name="layout" content="main-ads.gsp"/>.

http://docs.codehaus.org/display/GRAILS/Views+and+Layouts

Rob Hruska
+1  A: 

Starting with Grails 1.1 there's now an <g:include> tag which pretty much exactly does what you want.

It still makes sense to look into the above options re layout / templates since they will be more efficient in most cases.