Grails Layouts - Kind of a Nested Layout Problem, Conditional Content

769 views Asked by At

I have the following (simplified) layout:

<header>
  ... more stuff here
  <nav id="nav">
    <div class="content_wrapper">
      <ul id="menu">
        <li>dashboard</li>
      </ul>
    </div>
  </nav>
</header>
<div id="main_content">
  <div class="content_wrapper">
    <g:layoutBody/>
  </div>
</div>

My problem is that the "menu" nav is dependent on what is rendered in the layoutBody. Not only will the text "dashboard" change to "login", "support", etc but on some pages there are buttons, links, etc. Is there any way in Grails, without changing the layout of the page, to dynamically render this NAV element based on the layoutBody? I've read the docs including the content block section, but they don't solve the problem because they are still pre-determined in the main layout.

1

There are 1 answers

1
fabien7474 On BEST ANSWER

I have the same problem on my grails webapp. Here is the way I have solved it. In your layout:

<nav id="nav">
    <div class="content_wrapper">
      <ul id="menu">
        //Here you can insert whatever you have in all your pages
        //Then below you can insert custom content depending on main body
        <li><g:pageProperty name="page.nav-content"/></li>
      </ul>
    </div>
</nav>

Then in your main views (like home.gsp) requiring additional modules (like login module), you can include something like:

<content tag="nav-content">
   <g:include controller="nav" action="login"/>
</content>