2

我一直在闲逛,但找不到任何文档或示例代码。

介绍:

我有一个具有此键/值映射的 ftl 页面:

  • roomType.description["ES"] = "texto"
  • roomType.description["EN"] = "一些文字"
  • roomType.description["PT"] = "texto"

问题:

如何将地图作为参数传递给freemarker宏?

示例代码:

宏声明

<#macro descriptionMacro firstLang descriptionText>
    <#-- SOME CODE -->
    <textarea>
        <#if descriptionText[firstLang]??>
            ${descriptionText[firstLang]?trim}
        </#if>
    </textarea>
    <#-- SOME OTHER CODE -->
</#macro>

宏调用(不工作)

<@descriptionMacro firstLang="es" descriptionText=roomType.description/>
4

1 回答 1

3

我在您的代码中看到的唯一问题是映射中的键是大写的(“EN”、“ES”、“PT”),并且您尝试使用小写键“es”访问模板中的值”。

Map除此之外,我认为使用 a作为参数没有任何限制。

例如给定这张地图:

  Map<String, String> description = new HashMap<>();
  description.put("en", "Some text");
  description.put("es", "Testo");
  description.put("fr", "Texte");

  Map<String, Object> data = newHashMap();
  data.put("description", description);

  Template template = getTemplate();
  Writer out = new StringWriter();
  template.process(data, out);

这个模板:

<#macro printDescription lang data>
    Description = ${data[lang]}
</#macro>
<@printDescription lang="es" data=description />

输出是:

Description = Testo
于 2013-04-17T11:27:00.740 回答