Sitemesh布局不适用于Grails中的g.include标记

时间:2011-09-08 15:25:11

标签: layout grails gsp sitemesh

我正在渲染一个结合了g.include调用和sitemesh布局的视图。 视图将是这样的: myview.gsp

<html>
    <head>
        <meta name="layout" content="checkout" />
    </head>
    <body>...
在正文中有一个调用:

g.include(controller:"mycontroller", action:"myaction")

问题是sitemesh布局是从不应用。如果我删除include调用就行了。

我还没有在网站上找到对此问题的引用。 有没有人找到解决这个问题或提示的方法,将不胜感激!

由于

-Pablo Duranti

1 个答案:

答案 0 :(得分:1)

我的索引文件就像底层:

<html>
<head>
    <title>App Store For Publish, Download Android Apps</title>
    <meta name="layout" content="main" />
    <parameter name="sideBarSetting" value="main"/>
</head>
<body>
    <g:if test="${flash.message}">
        <div class="message">${flash.message}</div>
    </g:if>
    <g:announcements/>
    <g:include controller="cache" action="showFeatured"/>
    <g:include controller="cache" action="latestProducts"/>
    <div class="push"></div>
    <g:include controller="cache" action="mostPopular"/>
    <div class="push"></div>        
    <g:include controller="cache" action="allCategories"/>
</body>

它适用于Grails 1.0,1.2.2和现在的1.3.7。

在您尝试包含的每个操作中,您无法渲染视图,而是渲染模板。模板文件只能包含HTML片段,不能包含头部,元素布局等等。

在我的缓存控制器

def latestProducts = {
    cache shared:true, validFor: 300
    def htmlCacheManager = HtmlCacheManager.getInstance()
    def key = 'latestProducts'
    def content = htmlCacheManager.getHtmlContent(key)
    if (!content) {
        def products = productService.get5LatestProducts(params)
        if (products){
            content = g.render(template:'/common/product/productLatestListTemplate', model:['productInstanceList' : products, 'type':'latest'])
            htmlCacheManager.store(key, content, Boolean.TRUE)
        } else {
            log.debug('No latest product found')
        }
    }
    render content ?: ''
}

模板文件:

    <div class="list">
<fieldset>
<legend><g:message code="product.latest"/>  <g:link action="feed" controller="product" params="['type':type]" target="_blank"><img src="${resource(dir:'images', file:'feed-icon.gif')}"  height='16' width='16' alt="Feeds"/></g:link></legend>
    <g:each in="${productInstanceList}" var="product">
    <div class="product">
        <g:render template="/common/product/productSingleListTemplate" model="['product':product]" />
    </div>
    </g:each>
</fieldset>
</div>