格式化

几乎所有的模板语言都支持格式化,Beetl也不列外,如下例子Beetl提供的内置日期格式

  1. <% var date = date(); %>
  2. Today is ${date,dateFormat="yyyy-MM-dd"}.
  3. Today is ${date,dateFormat}
  4. salary is ${salary,numberFormat="##.##"}

格式化函数只需要一个字符串作为参数放在=号后面,如果没有为格式化函数输入参数,则使用默认值,dateFormat格式化函数默认值是local

Beetl也允许为指定的java class设定格式化函数,譬如已经内置了对java.util.Date,java.sql.Date 设置了了格式化函数,因此上面的例子可以简化为

  1. ${date,“yyyy-MM-dd”}

Beetl针对日期和数字类型提供的默认的格式化函数,在org/beetl/core/beetl-default.properties里,注册了

  1. ##内置的格式化函数
  2. FT.dateFormat = org.beetl.ext.format.DateFormat
  3. FT.numberFormat = org.beetl.ext.format.NumberFormat
  4. ##内置的默认格式化函数
  5. FTC.java.util.Date = org.beetl.ext.format.DateFormat
  6. FTC.java.sql.Date = org.beetl.ext.format.DateFormat
  7. FTC.java.sql.Time = org.beetl.ext.format.DateFormat
  8. FTC.java.sql.Timestamp = org.beetl.ext.format.DateFormat
  9. FTC.java.lang.Short = org.beetl.ext.format.NumberFormat
  10. FTC.java.lang.Long = org.beetl.ext.format.NumberFormat
  11. FTC.java.lang.Integer = org.beetl.ext.format.NumberFormat
  12. FTC.java.lang.Float = org.beetl.ext.format.NumberFormat
  13. FTC.java.lang.Double = org.beetl.ext.format.NumberFormat
  14. FTC.java.math.BigInteger = org.beetl.ext.format.NumberFormat
  15. FTC.java.math.BigDecimal = org.beetl.ext.format.NumberFormat
  16. FTC.java.util.concurrent.atomic.AtomicLong = org.beetl.ext.format.NumberFormat
  17. FTC.java.util.concurrent.atomic.AtomicInteger = org.beetl.ext.format.NumberFormat