开发者

How to trigger sub views in Grails

开发者 https://www.devze.com 2023-02-23 01:31 出处:网络
Bit of an issue as to not knowing which is the best way to trigger various templates in grails. I am trying to hard code the menus but still have different menus for different areas. I personally fin

Bit of an issue as to not knowing which is the best way to trigger various templates in grails. I am trying to hard code the menus but still have different menus for different areas. I personally find it a waste running queries on a database to construct a menu.

Quick View snippet and scenario. subnav is the dynamic menu.

<body>
  <div id='navigation'>
    Main menu list
  </div>
  <div id='subnav'>
    Render g开发者_如何转开发rails template base on path
    e.g. http://www.example.com/cars/ford => render ${ford} menu template
         http://www.example.com/cars/toyota => render ${toyota} menu template
  </div>
</body>

Now rendering the template is the easy part since I could just use a variable that follows the name of the template. e.g. <g:render template="/layouts/${submenu-name}"/>

What I'm not sure about is what is the best way of setting this variable. (good/evil practices)

Do I use a filter and fiddle around in the url/uri to get my location OR do I set it everytime in the controller when needed OR use something I have not thought of?

Thanks

EDIT:

Not sure if this is the best practice but lets say I have a "car template" and a "motorcycle template" then I just use a filter to change the model before the view gets to it.

E.g.

def filters = {
car(uri:'/car/*') {
    after = { model -> model.menuTpl = 'car' }
}
bike(uri:'/bike/*') {
    after = { model -> model.menuTpl = 'bike' }
}

And then in the main view:

<g:render template="/layouts/${menuTpl}" />


I don't see anything "evil" about your filter option. But just for the sake of giving options, here is what I am doing in one of my projects.

In my layout there is a header section in the body that contains a styled bar that goes across the page. This bar contains a heading like "Login", "Dashboard", "Support", etc. So in my login.gsp, I have the following:

<meta name="nav" content="login"/>

And in my main.gsp layout, I have the following:

<g:if test="${pageProperty(name:'meta.nav') == 'login'}">
   <ul id="menu">
      <li>Login</li>
   </ul>
</g:if>

I have several blocks checking the meta.nav. Some of them even drive more than just text. Some pull in templates.

0

精彩评论

暂无评论...
验证码 换一张
取 消