将格式化功能添加到Java Mustache中的模板。

huangapple go评论99阅读模式
英文:

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&lt;String, Object&gt; scopes = new HashMap&lt;String, Object&gt;();

//Add date 
scopes.put(&quot;number_to_format&quot;,BigDecimal.ONE);
scopes.put(&quot;currency&quot;, 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(&quot;template.mustache&quot;);
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

// 输入变量是一个传入的模板,你需要渲染它以获取值&lt;br/&gt;
因此再次使用相同的工厂和作用域对其进行渲染并获取值

HashMap&lt;String, Object&gt; scopes = new HashMap&lt;String, Object&gt;();

final MustacheFactory mf = new DefaultMustacheFactory();
// 添加日期
scopes.put(&quot;number_to_format&quot;, BigDecimal.ONE);
scopes.put(&quot;currency&quot;, new TemplateFunction() {
    public String apply(String input) {
        // 将输入渲染为模板以获取值
        Mustache mustache = mf.compile(new StringReader(input), &quot;&quot;);
        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(&quot;template.mustache&quot;);
mustache.execute(new PrintWriter(System.out), scopes).flush();

否则通过检查输入从HashMap获取值

if(input.equals(&quot;{{number_to_format}}&quot;)){
    input = scopes.get(&quot;number_to_format&quot;).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&lt;String, Object&gt; scopes = new HashMap&lt;String, Object&gt;();
		
final MustacheFactory mf = new DefaultMustacheFactory();
// Add date
scopes.put(&quot;number_to_format&quot;, BigDecimal.ONE);
scopes.put(&quot;currency&quot;, new TemplateFunction() {
	public String apply(String input) {
        //render the input as template to get the value
		Mustache mustache = mf.compile(new StringReader(input), &quot;&quot;);
		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(&quot;template.mustache&quot;);
mustache.execute(new PrintWriter(System.out), scopes).flush();

Otherwise get the value from HashMap by checking the input

if(input.equals(&quot;{{number_to_format}}&quot;)){
	input = scopes.get(&quot;number_to_format&quot;).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

huangapple
  • 本文由 发表于 2020年9月9日 08:59:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/63803356.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定