ASP 的 ContentPlaceHolder 的 Grails GSP 等价物是什么?
我一直在研究 Grails GSP 中的模板/布局概念。我一直在使用布局/内容块来模仿 ASP 的母版页行为。
例如,我在模板中使用标签
来留下一个“占位符”,可以使用
标签覆盖该占位符:
myTemplate.gsp:
<body>
<g:pageProperty name="page.topDiv" />
</body>
myPage.gsp:
<html>
<head>
<meta name="layout" content="myTemplate"></meta>
</head>
<body>
<content tag="topDiv">
My top div
</content>
</body>
</html>
这非常适合将内容“附加”到模板中的某个位置。然而,我确实想要在 ASP.NET 母版页中获得的行为...即提供某些内容的“默认”呈现,并允许可选的覆盖。在 ASP.NET 母版页中,它看起来像这样:
myMaster.master:
<asp:ContentPlaceHolder id="something" runat="server">
<div>Default text/html here</div>
</asp:ContentPlaceHolder>
someOtherPage.aspx:
<asp:Content contentPlaceHolderId="something" runat="server">
Overriden content here!! I don't need to override this though :)
</asp:Content>
我的问题:
我可以在 Grails 的 GSP 中执行相同的默认/覆盖行为吗?
I've been playing around a lot with the templating/layout concepts in Grails GSP. I've ben using layouts/content blocks for imitating ASP's master page behavior.
For example, I am using the tag <g:pageProperty />
in a template to leave a "placeholder" which can be overridden using a <content>
tag:
myTemplate.gsp:
<body>
<g:pageProperty name="page.topDiv" />
</body>
myPage.gsp:
<html>
<head>
<meta name="layout" content="myTemplate"></meta>
</head>
<body>
<content tag="topDiv">
My top div
</content>
</body>
</html>
This works great for "appending" content to some spot within a template. However, I really want the behavior I can get in ASP.NET's master pages... which is to provide a "default" rendering of some content, and allow optional overriding. In an ASP.NET Master page, it would look like this:
myMaster.master:
<asp:ContentPlaceHolder id="something" runat="server">
<div>Default text/html here</div>
</asp:ContentPlaceHolder>
someOtherPage.aspx:
<asp:Content contentPlaceHolderId="something" runat="server">
Overriden content here!! I don't need to override this though :)
</asp:Content>
My Question:
Can I do this same default/overriding behavior in Grails' GSP?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在几天内完成此任务。
g:pageProperty
相当于 Sitemeshdecorator:getProperty
标记,因此您可以使用default
属性来指示要使用的默认文本。例如:但是,我不知道有一种干净的方法来获取 HTML 内容。您可以使用
g:if
标记来测试该属性并指定默认行为(如果该属性不存在):默认内容也可以存在于外部模板 gsp 中。然后,可以使用
render
方法在g:pageProperty
的默认属性中显示该内容:在本例中,默认内容将位于
_topDiv 中.gsp
。There are a few different days you could accomplish this. The
g:pageProperty
is equivalent to the Sitemeshdecorator:getProperty
tag, so you can use adefault
attribute to indicate the default text to use. For example:However, I don't know of a clean way to get HTML content in there. You could use a
g:if
tag to test for that property and specify default behavior if it doesn't exist:The default content could also live in an external template gsp. The
render
method could then be used to display that content in the default attribute ofg:pageProperty
:Where in this case, the default content would be located in
_topDiv.gsp
.我想你可以尝试一下。
I think you can try instead.