Wednesday, May 2, 2007

Some FreeMarker operators

- Number
value?c converts a number to string for ``computer audience'' as opposed to human audience.e.g., number 3600 will be parsed as 3600 instead of 3,600

- Handle missing values
exp1!exp2 deprecates exp1?default(exp2)
exp1! deprecates exp1?if_exists (replace with empty string "", empty set etc if not exists)
exp1?? deprecates exp1?exists

Note:
product.color?default("red")

This examines if the product hash contains a subvariable called color or not, and returns "red" if not. The important point is that product hash itself must exist, otherwise the template processing will die with error.

(product.color)?default("red")
This examines if product.color exists or not. That is, if product does not exist, or product exists but it does not contain color, the result will be "red", and no error will occur.

No comments: