2

While reviewing some code in grails, I noticed that at a certain point specific code is reused in every controller and passed to the views to render a view section which can be normally be part of the main layout. The only problem is how to pass those values to the main layout from another point outside the specific controllers.

Any best-practice in dealing with similar cases of passing variables to the main layout?

4

1 回答 1

4

您为此使用过滤器

例如:

class MyFilters {   

    def filters = {
        all(controller:'*', action:'*') {
            before = { }

            after = { Map model ->
                // add your common data here
                model.commonData = ...          
                return true
            }

            afterView = { Exception e -> }
        }
    }
}

after控制器执行之后但在视图呈现之前调用闭包。您还可以更具体地了解过滤器配置 ( all(controller:'*', action:'*'))。例如,您可以排除某些控制器或类似的东西。

于 2013-09-05T07:50:55.563 回答