在自定义标记周围包装Groovy标记

时间:2013-01-24 23:13:10

标签: grails

基本上,我试图找到一种DRYer方式来表示这段代码:

<li><g:link controller="ActivityDashboard" action="summary">Summary</g:link></li>
<li><g:link controller="ActivityDashboard" action="user_details">User Details</g:link></li>
<li><g:link controller="ActivityDashboard" action="groups_monitors">Groups & Monitors</g:link></li>
<li><g:link controller="ActivityDashboard" action="Top Content">Top Content</g:link></li>
<li><g:link controller="ActivityDashboard" action="embedded_components">Embedded Components</g:link></li>
<li><g:link controller="ActivityDashboard" action="customer_summary">Customer Summary</g:link></li>

正如您所看到的,有很多重复,所以我创建了一个标记如下:

def navLink = { attrs, body ->
  out << "<li>"
  out << "<g:link controller='ActivityDashboard' action='${attrs.action}'>${attrs.title}"
  out << "</g:link></li>"
}

但是,输出html正好是:

<g:link action="summary" controller="ActivityDashboard">Summary</g:link>

我怎样才能渲染:

<a href="/engagementlevels/activityDashboard/summary">Summary</a>

加分点:我可以从标签访问页面的actionName属性吗?

1 个答案:

答案 0 :(得分:3)

您需要使用标记的g.link()版本,以便GString解析它并按照您的需要呈现输出。

out << g.link(attrs.title, controller: 'ActivityDashboard', action: attrs.action)