Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

freemarker - variable name with dot

Is it possible to refer a variable that contains dot in a freemarker template? (or '-' character)

e.g.:

Main.java:

public class Main {
    public static void main(String[] args) throws IOException, TemplateException {
        Configuration freemarkerConfig = new Configuration();
        freemarkerConfig.setClassForTemplateLoading(Main.class, "");
        Template template = freemarkerConfig.getTemplate("template.ftl");

        Map<String, String> data = new HashMap<String, String>();
        data.put("variable.name", "value");

        Writer out = new StringWriter();
        template.process(data, out);
        System.out.println(out.toString());
    }
}

template.ftl:

${variable.name}

This code throws exception:

The following has evaluated to null or missing:
==> variable  [in template "template.ftl" at line 1, column 3]

Is it possible to refer the variable "variable.name" in the tempalte file?

like image 661
nagy.zsolt.hun Avatar asked Mar 28 '14 16:03

nagy.zsolt.hun


1 Answers

It is possible with this syntax:

${.data_model["variable.name"]}

.data_model is a special variable you can use to access the data-model directly.

like image 158
obourgain Avatar answered Sep 28 '22 06:09

obourgain