Skip to content

Latest commit

 

History

History
315 lines (223 loc) · 8.1 KB

builtin_transformer.md

File metadata and controls

315 lines (223 loc) · 8.1 KB

Builtin Transformers

Rythm provides a set of built-in transformers to easy your work

Note, unless specified, all built-in transformers apply to Object type. In other words, you are use them to transform variable of any type, not only Strings.

If a variable been transformed is not a String, then it will be converted to String and then be processed by the transformer

[escape0]Escaping

Use escaping transformers when needed. (Usually you don't need to as Rythm is intelligent enough to escape your emissions with property escape scheme. Check it out at here).

[escape]escape()

Emit expression using the current escape scheme.

Since Rythm always escape expression output, meaning @foo.escape() has exactly the same effect of @foo.
@args Bar bar
@bar.raw()
@bar.escape()
@bar

[escapeHTML]escapeHTML()

Alias:

  • escapeHtml()
  • escape("html")

Emit expression using the html escape scheme.

@args Bar bar
@raw() {
@bar
@bar.escapeHtml()
@bar.escapeHTML()
@bar.escape("html")
}

[escapeXML]escapeXML()

Alias:

  • escapeXml()
  • escape("xml")

Emit expression using the xml escape scheme.

@args Bar bar
@raw() {
@bar
@bar.escapeXml()
@bar.escapeXML()
@bar.escape("xml")
}

[escapeJS]escapeJS()

Alias:

  • escapeJavaScript()
  • escape("js")

Emit expression using the js escape scheme.

@args Bar bar
@raw() {
@bar
@bar.escapeJS()
@bar.escapeJavaScript()
@bar.escape("js")
}

[escapeJSON]escapeJSON()

Alias:

  • escapeJson()
  • escape("json")

Emit expression using the json escape scheme.

@args Bar bar
@raw() {
@bar
@bar.escapeJSON()
@bar.escapeJson()
@bar.escape("json")
}

[escapeCSV]escapeCSV()

Alias:

  • escapeCsv()
  • escape("csv")

Emit expression using the csv escape scheme.

@args Bar bar
@raw() {
@bar
@bar.escapeCSV()
@bar.escapeCsv()
@bar.escape("csv")
}

[format]Format

[format_number]Format Number

  • format()
  • format(String pattern)
  • format(String pattern, Locale locale)

format a number using pattern and locale. Note this transformer only applies to Number type variable.

If locale is not specified, then a call to I18N.locale() is used to fetch the locale. If pattern is specified, the locale is used to get the DecimalFormatSymbols to construct the DecimalFormat object, otherwise, the locale is used to get NumberFormat object.

@args Number x, Number y, Number z
[@x]: @x.format()
---
[@y]: @y.format("###,000,000.00")
---
[@z]: @z.format("###,000,000.0000", Locale.SIMPLIFIED_CHINESE)

[format_date]Format Date/Time

  • format()
  • format(String pattern)
  • format(String pattern, Locale locale)
  • format(String pattern, Locale locale, String timezone)

format a date using pattern, locale and timezone string. Note this transformer only applies to java.util.Date type variable.

If locale is not specified, then a call to I18N.locale() is used to fetch the locale. If pattern is specified then it is used to construct the SimpleDateFormat to format the date; otherwise DateFormat.getDateInstance(DateFormat.Default, locale) will be used to create the date formatter. At last if timezone is specified it will be set to the date format.

@args Date x = new Date()
[@x]: @x.format()
---
[@x]: @x.format("yyyy-MM-dd")
---
[@x]: @x.format(null, Locale.SIMPLIFIED_CHINESE)
---
[@x]: @x.format(null, Locale.US, "GMT")

[format_currency]format currency

  • formatCurrency()
  • formatCurrency(String currencyCode)
  • formatCurrency(String currencyCode, Locale locale)

Format currency with currency code and locale specified. Unlike format() transformer for Number, formatCurrency does not require the variable type be Number or it's subtype, however formatCurrency is NOT null safe, so you need to use null-safe expression notation if you want to make it null safe.

@args Object o, Number n
[@o]: @o?.formatCurrency()
----
[@n]: @n?.formatCurrency()
See also

[string]String operations

Use string operation transformers to manupulate the output string.

[capitalizeWords]capitalizeWords()

Captialize the first character of each words. Words are separated by non digits-alphabetic characters.

@args Bar bar, String s
@bar
@bar.capitalizeWords()
------------
@s
@s.capitalizeWords()

[noAccents]noAccents()

Replace accent character (usually found in European languages) with corresponding non-accent character

@args Bar bar
@bar
@bar.noAccents()

[lowerFirst]lowerFirst() and capFirst()

Make the first letter of the object to be lowercase or uppercase

@args Bar bar, String s
@bar
@bar.lowerFirst()
---
@s
@s.capFirst()

[camelCase]camelCase()

Change each word from underscore style to camel case style

@args Bar bar
@bar
@bar.camelCase()

[divide]divide()

TBD

[lowerCase]lowerCase()

TBD

[upperCase]upperCase()

TBD

[misc]Misc utilities

[len]len()

TBD

[nl2br]nl2br()

Change line break to html <br/> element

@args Bar bar
@bar
@bar.nl2br()

[urlEncode]urlEncode()

Encode URL using UTF-8

@args Bar bar
@bar
@bar.urlEncode()

[i18n]i18n()

Internationalization a string or string of an object

@args String x
//-- default locale
@x: @x.i18n()

// -- locale: en
@locale("en"){
@x: @x.i18n()
}

// -- locale: zh_CN
@locale("zh_CN") {
@x: @x.i18n()
}

[join]join an Iterable

  • join() - join use ,
  • join(String separator)
  • join(char separator)

Join an Iterable with default , or specified separator.

@args Bar bar
@bar
@bar.nl2br()

[see]See also