这个 给每个 用户 自定义 模板解析,提供了很大的灵活性,非常好
You only need to create a Java class extending play.templates.JavaExtensions.
package ext;
import play.templates.JavaExtensions;
public class CurrencyExtensions extends JavaExtensions {
public static String ccyAmount(Number number, String currencySymbol) {
String format = "'"+currencySymbol + "'#####.##";
return new DecimalFormat(format).format(number);
}
}
Each extension method is a static method and should return a java.lang.String to be written back in the page. The first parameter will hold the enhanced object.
Use your formatter like this:
<em>Price: ${123456.324234.ccyAmount()}</em>
Template extension classes are automatically detected by Play at start-up. You just have to restart your application to make them available.
自己的例子
public class a extends JavaExtensions {
public static String foo(String number) {
// 而且特别要注意 第一个 参数必须是对象,不能是基本数据类型,这由于模板语法是 第一个参数.foo() 这样来使用的
return number+"_that is over";
}
}
${'1'.foo()}
======================
经过开发实践发现 这种 扩展并不够强大
比如 ${user.permission.checkPer(1)} 这样是正确的
而${1.checkPer(user.permission)}就是错误的了。