开发者

Grails g:include can it be done?

开发者 https://www.devze.com 2023-03-13 00:39 出处:网络
Im wondering if it is possible to use g:include to include only the body contents of a given page. Say i have a main layout page as follows:

Im wondering if it is possible to use g:include to include only the body contents of a given page.

Say i have a main layout page as follows:

<html>
  <head>
    <title>My start page</title>
    <g:layoutHead>
  </head>
  <body>
    <g:layoutBody>
  </body>
</html>

Then a main page (index.gsp)

<html>
  <head>
    <!-- main layout reference -->
    <meta name="layout" content="main"/>
  </head>
  <body>
    THIS IS MY INDEX BODY CONTENT WITH AN INCLUDE
    <g:include controller="book" action="list"/>
    <g:link controller="book" action="list">See the full list!</g:link>
  </body>
</html>

And finally the book/list page

<html>
  <head>
    <!-- main layout reference -->
    <meta name="layout" content="main"/>
  </head>
  <body>
    <table>
    <g:each in="${books}">
      <tr>
        <td>${it.author}</td>
        <td>${it.title}</td>
        <td>${it开发者_开发技巧.price}</td>
      </tr>
    </g:each>
    </table>
  </body>
</html>

So what i want to achieve is that the main page (index.gsp) only includes the table defined in the book/list page. However, when i try this it includes the entire html defined (<html> tags and all).

is it possible to do this somehow? i've tried things like <g:include controller="book" action="list" view="someView.gsp"/> however this doesn't work. I really want to avoid having to add a book list logic to the "index controller" i want to reuse my existing controller.

I can't be the first person out there having this usecase, what solutions have you guys come up with?


You can use the applyLayout tag. Simply create an empty.gsp layout with only:

<g:layoutBody/>

And then decorate your include tag with applyLayout:

<g:applyLayout name="empty">
  <g:include controller="book" action="list"/>
</g:applyLayout>

See the entry on the Grails guide for further reference.


This is IMHO directly not possible. An idea would be to create a custom tag based on g:include, that strips out parts of the code by e.g. an xpath expression. I'm not aware that this already exists somewhere.

An alternative would be to refactor the body part of book's list.gsp into a template and reference that template from index.gsp using g:render. But this means that the data model has to be available in index.gsp context since g:render does not invoke a controller.

Side note: when using g:include it's a good idea to use the springcache plugin for page fragment caching.


Yes, but you need you need one more level in there. Look at Grails templates. Essentially, you'd have a template: _books.gsp containing:

   <table>
    <g:each in="${books}">
      <tr>
        <td>${it.author}</td>
        <td>${it.title}</td>
        <td>${it.price}</td>
      </tr>
    </g:each>
    </table>

Then your index would be:

<html>
  <head>
    <!-- main layout reference -->
    <meta name="layout" content="main"/>
  </head>
  <body>
    THIS IS MY INDEX BODY CONTENT WITH AN INCLUDE
    <g:render template="books">
    <g:link controller="book" action="list">See the full list!</g:link>
  </body>
</html>

And your list would be:

<html>
  <head>
    <!-- main layout reference -->
    <meta name="layout" content="main"/>
  </head>
  <body>
    <g:render template="books" />
  </body>
</html>

(My syntax may not be 100% right, since it's been a couple months since I've done this, but the idea behind templates are short, reusable pieces of GSP code that aren't meant to be displayed on their own.

0

精彩评论

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