英文:
Add formating function to Template in Java Mustache
问题
我有一个Java Mustache应用程序,我需要应用一个函数以货币格式呈现它。
我有我的模板
{{#currency}}{{number_to_format}}{{/currency}}
和我的函数
HashMap<String, Object> scopes = new HashMap<String, Object>();
// 添加日期
scopes.put("number_to_format", BigDecimal.ONE);
scopes.put("currency", new TemplateFunction() {
public String apply(String input) {
NumberFormat currency = NumberFormat.getCurrencyInstance();
return currency.format(new BigDecimal(input));
}
});
MustacheFactory mf = new DefaultMustacheFactory();
Mustache mustache = mf.compile("template.mustache");
mustache.execute(writer, scopes).flush();
我无法获得"input"变量中的值,我总是得到变量名"number_to_format"。如果我在函数中返回一个值,它将被呈现。
如何在输入中获取我的变量的数值?
英文:
I have a Java Mustache app and I need to apply a function to render it in currency format
I have my template
{{#currency}}{{number_to_format}}{{/currency}}
And my function
HashMap<String, Object> scopes = new HashMap<String, Object>();
//Add date
scopes.put("number_to_format",BigDecimal.ONE);
scopes.put("currency", new TemplateFunction() {
public String apply(String input) {
NumberFormat currency = NumberFormat.getCurrencyInstance();
return currency.format(new BigDecimal(input));
}
}
);
MustacheFactory mf = new DefaultMustacheFactory();
Mustache mustache = mf.compile("template.mustache");
mustache.execute(writer,scopes).flush();
I am unable to get the value in the "input" variable, i always get the variable name "number_to_format". If I return a value in the function it will be rendered.
How can i get the numeric value of my vairable in input?
答案1
得分: 1
// 输入变量是一个传入的模板,你需要渲染它以获取值<br/>
因此,再次使用相同的工厂和作用域对其进行渲染并获取值
HashMap<String, Object> scopes = new HashMap<String, Object>();
final MustacheFactory mf = new DefaultMustacheFactory();
// 添加日期
scopes.put("number_to_format", BigDecimal.ONE);
scopes.put("currency", new TemplateFunction() {
public String apply(String input) {
// 将输入渲染为模板以获取值
Mustache mustache = mf.compile(new StringReader(input), "");
StringWriter out = new StringWriter();
mustache.execute(out, scopes);
NumberFormat currency = NumberFormat.getCurrencyInstance(Locale.US);
return currency.format(new BigDecimal(out.toString()));
}
});
Mustache mustache = mf.compile("template.mustache");
mustache.execute(new PrintWriter(System.out), scopes).flush();
否则通过检查输入从HashMap获取值
if(input.equals("{{number_to_format}}")){
input = scopes.get("number_to_format").toString();
}
NumberFormat currency = NumberFormat.getCurrencyInstance(Locale.US);
return currency.format(new BigDecimal(input));
否则移除"{{"和"}}"并将其用作哈希映射的键
英文:
The input variable is an incoming template, you need to render it to get the value<br/>
Therefore again render it with same factory and scopes and get the value
HashMap<String, Object> scopes = new HashMap<String, Object>();
final MustacheFactory mf = new DefaultMustacheFactory();
// Add date
scopes.put("number_to_format", BigDecimal.ONE);
scopes.put("currency", new TemplateFunction() {
public String apply(String input) {
//render the input as template to get the value
Mustache mustache = mf.compile(new StringReader(input), "");
StringWriter out = new StringWriter();
mustache.execute(out, scopes);
NumberFormat currency = NumberFormat.getCurrencyInstance(Locale.US);
return currency.format(new BigDecimal(out.toString()));
}
});
Mustache mustache = mf.compile("template.mustache");
mustache.execute(new PrintWriter(System.out), scopes).flush();
Otherwise get the value from HashMap by checking the input
if(input.equals("{{number_to_format}}")){
input = scopes.get("number_to_format").toString();
}
NumberFormat currency = NumberFormat.getCurrencyInstance(Locale.US);
return currency.format(new BigDecimal(input));
Otherwise remove the "{{" and "}}" and use it as a key to hashmap
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论