views:

1516

answers:

1

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}.)

+4  A: 

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

Colin Harrington
Thanks! I realized this myself when I was re-reading "the definitive guide to grails" It turns out that if I want to have a ".gsp" named "index.gsp" under a view name home, it will find the "home" directory automatically based on the name of the controller, and all I have to do is say render (view: "index") view doesn't really refer to a "view", it refers to a .gsp file under the view directory named the same as the controller. Sometimes this "Convention-based" programming can screw up a noob used to other terminology.
ראובן