So I have a navigation template (/common/_navigation.gsp) that is getting rendered in the Grails projects main Layout file (/layouts/main.gsp). How do I pass some kind of variable/parameter/arg from the individual view files to layout and eventually into the navigation template? I just want the right tab to be highlighted when I'm on a page.
(We've already tried using the Grails Navigation Plugin. Since we have different tabs that point to the same controllers (same view, different filter) it breaks down.)
The pattern I like, uses the pageProperty as follows. In the layout, I reference the pageProperty like so:
The layout gsp
... and within the
<head>
section of the specific gsp page (I found that it did NOT work outside the head section), I declare the value like so:The page gsp
Resulting HTML
In addition, I can inject a value from the controller model into pageProperty like so:
Controller
The layout gsp (using the same layout as above)
The page gsp
Resulting HTML
just use the model parameter for Example: view:
_template:
You can simply reach any variable in any template in the page by getting it from params. Just type
${params.variable}
in layout and you will get your variable.I use an arguably simpler method. Just define a variable at request scope in the individual view before you call your layout. It will be available in all templates used in the request, including the layout and any called via
<g:render>
Then just reference the variable as you would any other in your layout or other templates pulled in by your view
You'd need to use a page property: http://grails.org/doc/1.1.1/ref/Tags/pageProperty.html
Then pass it into the render tag using the model param.
cheers
Lee
I do this pattern all the time. In my view, I can attach a property to the page either manually or by using the parameter tag in the view that I'm rendering. Its not documented in the Grails user guide, but its super handy.
Then I would access the page property by using the pageProperty tag.
The layout doesn't need to handle this variable at all :-)