[英]How to trigger sub views in Grails
Bit of an issue as to not knowing which is the best way to trigger various templates in grails. 有点不知道哪个是触发grails中各种模板的最佳方法的问题。 I am trying to hard code the menus but still have different menus for different areas.
我正在尝试对菜单进行硬编码,但仍然针对不同的区域设置了不同的菜单。 I personally find it a waste running queries on a database to construct a menu.
我个人发现在数据库上构建菜单是一种浪费的运行查询。
Quick View snippet and scenario. 快速查看代码段和方案。 subnav is the dynamic menu.
subnav是动态菜单。
<body>
<div id='navigation'>
Main menu list
</div>
<div id='subnav'>
Render grails template base on path
e.g. http://www.example.com/cars/ford => render ${ford} menu template
http://www.example.com/cars/toyota => render ${toyota} menu template
</div>
</body>
Now rendering the template is the easy part since I could just use a variable that follows the name of the template. 现在渲染模板很容易,因为我可以使用一个跟随模板名称的变量。 eg
<g:render template="/layouts/${submenu-name}"/>
例如
<g:render template="/layouts/${submenu-name}"/>
What I'm not sure about is what is the best way of setting this variable. 我不确定的是设置此变量的最佳方法是什么。 (good/evil practices)
(善恶行为)
Do I use a filter and fiddle around in the url/uri to get my location OR do I set it everytime in the controller when needed OR use something I have not thought of? 我是否使用过滤器并在url / uri中进行调整以获取我的位置或者我是否每次都在控制器中设置它或者使用我没想过的东西?
Thanks 谢谢
EDIT:
编辑:
Not sure if this is the best practice but lets say I have a "car template" and a "motorcycle template" then I just use a filter to change the model before the view gets to it. 不确定这是否是最好的做法,但是假设我有一个“汽车模板”和“摩托车模板”,那么我只是使用过滤器在视图到达之前更改模型。
Eg 例如
def filters = {
car(uri:'/car/*') {
after = { model -> model.menuTpl = 'car' }
}
bike(uri:'/bike/*') {
after = { model -> model.menuTpl = 'bike' }
}
And then in the main view: 然后在主视图中:
<g:render template="/layouts/${menuTpl}" />
I don't see anything "evil" about your filter option. 我没有看到任何关于你的过滤器选项的“邪恶”。 But just for the sake of giving options, here is what I am doing in one of my projects.
但只是为了给出选项,这就是我在我的一个项目中所做的事情。
In my layout there is a header section in the body that contains a styled bar that goes across the page. 在我的布局中,正文中有一个标题部分,其中包含一个穿过页面的样式栏。 This bar contains a heading like "Login", "Dashboard", "Support", etc. So in my login.gsp, I have the following:
此栏包含“登录”,“仪表板”,“支持”等标题。因此,在我的login.gsp中,我有以下内容:
<meta name="nav" content="login"/>
And in my main.gsp layout, I have the following: 在我的main.gsp布局中,我有以下内容:
<g:if test="${pageProperty(name:'meta.nav') == 'login'}">
<ul id="menu">
<li>Login</li>
</ul>
</g:if>
I have several blocks checking the meta.nav. 我有几个块检查meta.nav。 Some of them even drive more than just text.
他们中的一些甚至不仅仅是文本。 Some pull in templates.
一些拉模板。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.