How do I pass a variable into grails template from

2019-03-12 16:23发布

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

7条回答
We Are One
2楼-- · 2019-03-12 16:55

The pattern I like, uses the pageProperty as follows. In the layout, I reference the pageProperty like so:

The layout gsp

<body id="${pageProperty(name: 'page.pageType')}">
    <g:render template="/layouts/header" />
    <g:layoutBody/>
    <g:render template="/layouts/copyright" />
</div>

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

<head>
    <meta name="layout" content="main"/>
    <parameter name="pageType" value="homePg" />
</head>

Resulting HTML

<body id="homePg">
    ... header, body and footer ...
</body>

In addition, I can inject a value from the controller model into pageProperty like so:

Controller

def index() {
    model: [modelPageType: 'adminPg']
}

The layout gsp (using the same layout as above)

<body id="${pageProperty(name: 'page.pageType')}">
    <g:render template="/layouts/header" />
    <g:layoutBody/>
    <g:render template="/layouts/copyright" />
</div>

The page gsp

<head>
    <meta name="layout" content="main"/>
    <parameter name="pageType" value="${modelPageType}" />
</head>

Resulting HTML

<body id="adminPg">
    ... header, body and footer ...
</body>
查看更多
时光不老,我们不散
3楼-- · 2019-03-12 16:59

just use the model parameter for Example: view:

<g:render template="../path/template" model="[header:'test', headers:['a','b'], values:[['a':1,'b':2],['a':3,'b':4]] ]"/>

_template:

<br/>
    <h3>${header}</h3>
    <table>
      <thead>
        <tr>
          <g:each in ="${headers}" var="he">
            <th>${he}</th>
          </g:each>
        </tr>
      </thead>
      <tbody>
        <g:each in ="${values}" var="v">
              <tr>
                <g:each in ="${headers}" var="h">
                  <td>${v[h]}</td>
                </g:each>
              </tr>
          </g:each>
      </tbody>
    </table>
查看更多
啃猪蹄的小仙女
4楼-- · 2019-03-12 17:02

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.

查看更多
我只想做你的唯一
5楼-- · 2019-03-12 17:03

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>

<g:set var="SOMEVARIABLE" value="SOMEVALUE" scope="request"/>
<meta name="layout" content="yourlayout">

Then just reference the variable as you would any other in your layout or other templates pulled in by your view

${SOMEVARIABLE}  or <g:if test="${SOMEVARIABLE}">, etc., etc
查看更多
SAY GOODBYE
6楼-- · 2019-03-12 17:06

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

查看更多
老娘就宠你
7楼-- · 2019-03-12 17:08

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.

<parameter name="foo" value="bar" />

Then I would access the page property by using the pageProperty tag.

<g:set var="activeNavItem" value="${pageProperty(name: 'page.foo')}"/>

The layout doesn't need to handle this variable at all :-)

查看更多
登录 后发表回答