0

我在“grails-app/views/teamplates”目录中创建了一个文件“_emailTemplate”。它是一个html模板文件,文件内容如下,

<html>
  <strong>Client: </strong>${client}<br/>
  <strong>Training: </strong>${training}<br/>
  <strong>Dates: </strong>${dates}<br/>
</html>

我想加载此文件并用特定值替换占位符,例如

${client} with the value of client variable etc.

这个怎么做。

4

1 回答 1

2

您可以将此模板呈现为字符串。在任何控制器中,您都可以使用以下代码:

def output = g.render(template: "/templates/emailTemplate", model: [client: 'John', training: 'Tennis', dates: 'tomorrow'])

g是一个注入的 RenderTagLib 实例。如果你想在服务中使用它,你必须为自己创建一个实例,如下所示:

def renderTagLib = new RenderTagLib()
def output = renderTagLib.render(template: "/templates/emailTemplate", model: [client: 'John', training: 'Tennis', dates: 'tomorrow'])
于 2012-07-16T13:41:30.690 回答