如何将一个变量从布局文件传递给Grails模板? [英] How do I pass a variable into grails template from a Layout file?

查看:130
本文介绍了如何将一个变量从布局文件传递给Grails模板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我有一个导航模板(/common/_navigation.gsp),它在Grails项目主布局文件(/layouts/main.gsp)中呈现。我如何将各种视图文件中的某种变量/参数/ arg传递给布局并最终导入导航模板?我只想在页面上突出显示正确的选项卡。



(我们已经尝试过使用Grails导航插件,因为我们有不同的选项卡,指向相同的控制器(相同的视图,不同的过滤器)。)

解决方案

我一直都在使用这种模式。在我看来,我可以手动或通过使用我正在渲染的视图中的参数标记将属性附加到页面。它没有在Grails用户指南中记录,但它的超级方便。

 < parameter name =foovalue =bar /> 

然后,我将通过使用 pageProperty标签

 < g:set var =activeNavItemvalue =$ {pageProperty(name:'page.foo')} /> 

布局根本不需要处理这个变量: - )


So I have a navigation template (/common/_navigation.gsp) that is getting rendered in the Grails projects main Layout file (/layouts/main.gsp). How do I pass some kind of variable/parameter/arg from the individual view files to layout and eventually into the navigation template? I just want the right tab to be highlighted when I'm on a page.

(We've already tried using the Grails Navigation Plugin. Since we have different tabs that point to the same controllers (same view, different filter) it breaks down.)

解决方案

I do this pattern all the time. In my view, I can attach a property to the page either manually or by using the parameter tag in the view that I'm rendering. Its not documented in the Grails user guide, but its super handy.

<parameter name="foo" value="bar" />

Then I would access the page property by using the pageProperty tag.

<g:set var="activeNavItem" value="${pageProperty(name: 'page.foo')}"/>

The layout doesn't need to handle this variable at all :-)

这篇关于如何将一个变量从布局文件传递给Grails模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆