I can’t get the grails controller “render” method to work with an explicit template

grailsgroovygsptemplates

I'm just getting started with grails, and I'm having an issue.

I have a "controller" and "view" for the projects home page (there's no model for the home page)

I called the view "index.gsp", and put it in a directory views/home

However, no matter what I do, grails is trying to read the page "home.gsp" (and then home.jsp), despite me having explicitly specified the index with the "template" attribute in the render call.

class HomeController {
    String someparameter = "xyzzy"
    def index = { 
        render(view:"home", template:"index")  // I also tried "index.gsp" and "home/index.gsp"
    }
}

I think I may be using the "template" attribute wrong, because I only see it used in examples for view-less template rendering. However the documentation gives no such limitation.

Is there any way to explicitly specify the name of a template? I just caved in and renamed it "home.gsp", but I'd like to understand what's going wrong.

(The home page in this application has no "model". Grails will use the controller has the model. In this example, you can access "someparameter" in the gsp template as ${someparameter}.)

Best Answer

I think you may be misunderstanding what a Grails template is. Think of a template as a reusable fragment. A template is a GSP that starts with an underscore like _menu.gsp that you would typically render from within another GSP with the a tag like <g:render template="menu"/>.

It doesn't make sense to render both a view and a template at the same time. They are mutually exclusive at this point.

Are you looking to implement a Layout? If so, see the docs or the grails.org explaination.

Basically, your view you would have a <meta name="layout" content="main"> tag in the <head/> tag of your view -- which indicates that the view will be meshed together with the main layout located in grails-app/views/layouts/main.gsp

Related Topic