g:render 标签将 body() 呈现为纯文本

g:render tag renders body() as plain text

我正在尝试在 GSP 页面中呈现模板

模板:

<div class="container">
  ${body()}
</div>

模板调用:

<g:render template="/shared/wrapperTemplate">
  <g:textField name="${property}" value="${value}" id="${property}id" class="form-control"/>
</g:render>

body() 被正确评估并呈现

<input type="text" name="name" value="" id="nameid" class="form-control" />

但是当将它传递给模板时,它被引号包围并且不显示输入字段,而是将 html 输入作为字符串打印到 html 页面

我也试过写 TagLib

def fieldTemplate = { attrs, body ->
    out << render(template: "/shared/wrapperTemplate",  model: [content: body()])
}

但是结果是一样的(当然我不得不更改标签调用)

想法是为字段插件中的所有 _wrapper.gsp 模板重复使用模板 <div> 的格式部分,而不是复制粘贴它。上面的案例是简化的,但是我使用推特 Bootstrap 并且有一堆我不想复制的行。

_fields/default/_wrapper.gsp:

<div class="form-group ${hasErrors(bean:bean,field:property,'has-error')}">
  <label for="${property}id" class="col-sm-2 control-label">${label}</label>
  <div class="col-sm-10">
    <g:textField name="${property}" value="${value}" id="${property}id" class="form-control" />
  </div>
</div>

_fields/date/_wrapper.gsp:

<div class="form-group ${hasErrors(bean:bean,field:property,'has-error')}">
<label for="${property}id" class="col-sm-2 control-label">${label}</label>
  <div class="col-sm-10">
    <g:datePicker name="${property}" value="${value}" precision="day" id="${property}id" class="form-control" />
  </div>
</div>

我无法理解您的以下几行post

but when passing it to the template, it is surrounded by the quotes and instead of displaying input field, it prints the html input as string to the html page

在这种情况下,它是什么,请分享不起作用的确切代码。 您共享的第一个示例对您有用,在第二个示例中,您将内容作为参数传递。当您传递参数时,您需要更改代码

${body()} 

${raw(content)}

我仍然不确定不起作用的确切代码是什么,只是胡乱猜测。