0

是否可以在 ftl 中命名一些块并将它们放在 java 端?例如像语法不正确的东西

#emailSubject[
This is email subject]
#emailMessage
[Email multi-
line message!!
Hi all]

和java端看起来像

template.process("template.ftl", resultModelOrSomethingElse);
String emailSubject = resultModelOrSomethingElse.getEmailSubject();
String emailMessag = resultModelOrSomethingElse.getEmailMessage();
4

1 回答 1

1

开箱即用,您可以执行以下操作:

<#assign emailSubject>This is the email subject</#assign>

<#assign emailMessage>
  This is the email message...
</#assign>

进而:

// Same as template.process, but you will have the Environment:
Environment env = template.createProcessingEnvironment(dataModel, out);
env.process();

// Extract top-level variables:
TemplateModel emailSubject = env.getVariable("emailSubject");
TemplateModel emailMessage = env.getVariable("emailMessage");

(如果你会做很多事情,你可能想在模板中简化它。就像<@emailSubject>This is the email subject</@>更简洁和更简单,因为如果有人在“emailSubject”变量名中犯了错误,它会立即失败。)

于 2013-03-20T22:22:18.367 回答