代码之家  ›  专栏  ›  技术社区  ›  maerics

Grails控制器可以选择布局吗?

  •  5
  • maerics  · 技术社区  · 14 年前

    Grails布局似乎是一个逻辑选择,但是Grails控制器是否可以显式地命名将用于呈现的布局?理想情况下 layout 选择 render 方法,每个RubyonRails,但我看不到类似的东西。

    applyLayout 方法传递布局的名称,但这要求每个GSP页显式请求布局(每页烦人的开销),而不是使用 Layout by Convention

    有什么想法吗?

    4 回复  |  直到 14 年前
        1
  •  5
  •   Ted Naleid    14 年前

    为什么不直接在模型中传递它并在决定布局的元标记中呈现它呢?

    <meta name="layout" content="${myValueFromController}"/>
    

    我还没试过,但我想可以。

        2
  •  3
  •   Burt Beckwith    14 年前

    我不知道如何在每个动作中执行,但是可以在控制器级别指定它,例如。

    class FooController {
    
       static layout = 'cms'
    
       def index = {}
       def foo = { ... }
       def bar = { ... }
    }
    
        3
  •  2
  •   codeporn    14 年前

    嘿,我想我有个办法给你:

    普惠制食品:

    <meta name="layout" content="${actionLayout}" />
    

    FooController.groovy:

    class FooController {
    
      def index = { 
        // do awesome stuff
      }
    
      def afterInterceptor = { model ->
        model.actionLayout = actionName}
      }
    }
    

    现在你唯一要做的就是像操作一样命名你的布局或者创建一些其他的命名逻辑。

        4
  •  2
  •   James    14 年前

    也许我错过了什么,但这难道不能用一点爱来轻松解决吗。。。?

    例如。

    <g:if test="${controllerName == 'xyzController'}">
        <meta name="layout" content="xyzLayout"/>
    </g:if>
    <g:else>
        <meta name="layout" content="abcLayout"/>
    </g:else>
    

    我使用类似的方法来确定哪个选项卡应该在我的布局中应用“selected”类。这允许我在仍然突出显示的情况下,将所有的导航html保存在布局中。和你要求的有点不同,但似乎可以( )还在工作。。。