如何在 Grails 模板 body() 中分配 GSP 变量?
在我的 Grails 1.3.7 应用程序中,我想使用模板从视图中提取周围的 HTML。但是,GSP 变量赋值在包含的 body() 中不起作用。我怎样才能做类似以下的事情?
_aTemplate.gsp:
<div class="example">
<% out << body() %>
</div>
aView.gsp:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="layout" content="main" />
</head>
<body>
<g:set var="foo" value="${42}"/>
<% assert foo == 42 : foo %>
<tmpl:/aTemplate>
<g:set var="bar" value="${6}"/>
<% assert bar == 6 : bar %>
</tmpl:/aTemplate>
</body>
</html>
bar
的赋值不起作用:bar
断言失败,当我得到 http://localhost:8080/myApp/aView.gsp
org.codehaus.groovy.grails.web.taglib.exceptions.GrailsTagException:
Error executing tag<g:render>: Assertion failed:
(bar == 6). Values: bar = null
at /Users/jbeutel/proj/grailsSandboxes/myApp/grails-app/views/aView.gsp:13
我该如何制作模板 body() 工作正常吗?
或者,是否有其他方法可以在保持平衡的同时剔除周围的 HTML?我的用例不是顶级的,所以我没有尝试使用布局。如果我使用 TagLib 闭包而不是模板文件,则变量分配有效,但我不想将大量 HTML 放入闭包中,因此无论如何我都需要将不平衡的 HTML 放入模板文件中(即,在和身体之后)。有更好的办法吗?
In my Grails 1.3.7 app, I'd like to use a template to factor out surrounding HTML from views. But, GSP variable assignment isn't working in the contained body(). How can I do something like the following?
_aTemplate.gsp:
<div class="example">
<% out << body() %>
</div>
aView.gsp:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="layout" content="main" />
</head>
<body>
<g:set var="foo" value="${42}"/>
<% assert foo == 42 : foo %>
<tmpl:/aTemplate>
<g:set var="bar" value="${6}"/>
<% assert bar == 6 : bar %>
</tmpl:/aTemplate>
</body>
</html>
The assignment of bar
doesn't work: the bar
assertion fails, when I get http://localhost:8080/myApp/aView.gsp
org.codehaus.groovy.grails.web.taglib.exceptions.GrailsTagException:
Error executing tag<g:render>: Assertion failed:
(bar == 6). Values: bar = null
at /Users/jbeutel/proj/grailsSandboxes/myApp/grails-app/views/aView.gsp:13
How can I make the template body() work as normal?
Alternatively, is there some other way I could factor out surrounding HTML while keeping it balanced? My use case isn't at the top level, so I haven't tried using layouts. The variable assignment works if I use a TagLib closure instead of a template file, but I don't want to put a lot of HTML into a closure, so I would need to put unbalanced HTML into template files anyway (i.e., separate templates before and after the body). Is there a better way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以将变量从 .gsp 传递到共享模板中,然后它可以按如下方式考虑这些模板。我这样做是为了隐藏公共标头中的“登录”链接(如果它们位于 login.gsp 页面上
,然后位于 _header.gsp 中)
You can pass variables from your .gsp into shared templates that it can then consider as follows. I do this to hide a 'login' link in my common header if they are on the login.gsp page
then in the _header.gsp