2012-03-25 39 views
5

我在我的main.gsp中包含了部分模板。使用視圖下的部分模板根

對於網站上的每個控制器/頁面,此部分內容略有不同。 因此,我將爲每個視圖目錄分別提供_headerDetails.gsp

這工作正常,除了默認應用程序index.gsp。 當我包括_headerDetails.gsp下根視圖目錄,我得到以下錯誤:

org.codehaus.groovy.grails.web.pages.exceptions.GroovyPagesException: Error processing GroovyPageView: Template not found for name [headerDetails] and path [//_headerDetails.gsp] 

是否Grails的不允許在根目錄下的諧音?

Main.gsp

<html> 
    <head> 
     <g:layoutTitle/> 
     <r:layoutResources/> 
     <link href="${resource(dir: 'css', file: 'style.css')}" type="text/css" rel="stylesheet"> 
    </head> 

    <body class="home"> 

     <div id="wrapper"> 

      <div id="page_top"></div> 

      <div id="content"> 
       <g:render template="/common/header" /> 

       <g:render template="headerDetails" /> 

       <br class="clear" /> 

       <g:layoutBody/> 

       <br class="clear" /> 

      </div> 

      <div id="page_bottom"></div> 

      <g:render template="/common/footer" /> 

     </div> 

     <r:layoutResources/> 

    </body> 
</html> 
+0

這個問題,它的答案,幫助我。未能看到它是如何「太本地化」。無論如何感謝過度緩和。 – 2014-07-09 13:33:29

回答

5

是它headDetailsheaderDetails ??

如果它沒有錯別字,請嘗試在模板名稱前使用/以獲得根目錄!

+1

這是我修復了修煉者的錯字。找出問題,看答案。你的答案一半解決了我的問題,謝謝。 – 2012-03-27 10:52:58

+0

使用斜槓前綴可以正常工作,謝謝 – peterp 2013-07-03 14:55:21

3
<g:if test="${params.action.equals('')}"> 
    <g:render template="/headerDetails" /> 
</g:if> 
<g:else> 
    <g:render template="headerDetails" /> 
</g:else> 
1

參見 「模板基礎知識>共享模板」 Grails的文檔的:

http://grails.org/doc/2.0.x/guide/theWebLayer.html#viewsAndTemplates

In this case you can place them in the root views directory at grails-app/views or any subdirectory below that location, and then with the template attribute use an absolute location starting with / instead of a relative location. For example if you had a template called grails-app/views/shared/_mySharedTemplate.gsp , you would reference it as:

<g:render template="/shared/mySharedTemplate" />