8.4.2 Expression language
Expressions can be used to insert dynamic content into the generated output or to provide arguments to template instructions (e.g. the "test" attribute of an <if> instruction).
Kauri template supports two expression languages:
- EL (= same as in JSP)
- Groovy (which is actually a full programming language)
Normally, you should have enough with EL and never touch Groovy.
Groovy offers a powerful escape for those who know what they're doing.
8.4.2.1 EL
The default expression language is the Unified Expression Language (EL), which is the same as used by JSP.
You can type your EL expressions using the ${...} or #{...} delimiter.
Note: when just using the expression language (thus no template directives), there is no need to declare the template namespace. The ${} and #{} delimiters will be picked up automatically when passed to the template engine. If you want to use the $ or # character as regular text, you have to escape it with a backslash: \$ and \# .
8.4.2.1.1 Expressions
Some of the supported expressions are:
- boolean
- true
- false
- logical operators
- and
- or, ||
- not, !
- relational operators
- ==, eq
- !=, ne
- <, lt
- >, gt
- <=, le
- >=, ge
- arithmetic operators
- binary: X <operator> Y
- +
- -
- *
- /, div
- %, mod
- unary: <operator> X
- -
- conditional operator
- <exp> ? <val1> : <val2>
- property resolution
- . , [ ]
- expr.identifier is equivalent to expr["identifier"]
You can access objects that are supplied to the context of the tempate engine by referencing their keys. With the dot-operator, you can access the properties of those objects, e.g.: you can use the expression ${request.clientInfo.acceptedMediaTypes} when there is a request object supplied to the execution context.
8.4.2.1.2 Functions
There are also some convenience functions available. These are called using prefix:name(), for example txt:concat('foo', 'bar').
8.4.2.1.2.1 Text functions
- txt:string(object)
- txt:toLowerCase(string)
- txt:toUpperCase(string)
- txt:concat(string1,string2)
- txt:trim(string)
- txt:length(string)
- txt:substring(string, beginIndex, endIndex)
- txt:left(string, length)
- txt:right(string, length)
- txt:startsWith(string, substring)
- txt:endsWith(string, substring)
- txt:contains(string, substring)
- txt:replace(string, regexp, replacement)
8.4.2.1.2.2 Math functions
- math:pi() -> returns the value of PI
- math:e() -> returns the value of E, the base of the natural logarithms
- math:sin(number)
- math:cos(number)
- math:min(number1,number2)
- math:max(number1,number2)
Actually all the functions of java.lang.Math are available as EL functions with prefix math. While this is very handy and powerful, you have to keep in mind that this also means that newly added math functions will be only available depending on the JRE used to run your application.
8.4.2.1.2.3 Date/time functions
- date:date(year, month, day) -> creates a java.util.Date holding the specified date
- date:time(hour, min, sec) -> creates a java.util.Date holding the specified time
- date:datetime(year, month, day, hour, min, sec) -> creates a java.util.Date holding the specified datetime
- date:format(date, pattern) -> format a date object with the specified pattern
- date:now(pattern) -> return the current datetime, formatted with the specified pattern
8.4.2.1.2.4 JSON functions
- json:serialize(obj) -> serializes a java object to a valid JSON string. This is most useful when your object is actually some sort of JSON object, but this function will also do an effort to serialize regular POJO's.
Remember to escape '<', '>' and '&' characters when using EL expressions in XML files.
8.4.2.1.3 Kauri-specific functions
These are functions only available when using the template engine within Kauri (using the kauri-template-service module).
8.4.2.1.3.1 publicUri(uri)
If the argument is a URI that uses the
Just as for
8.4.2.1.3.2 inSourceMode()
This function returns a boolean argument indicating if the module containing
the template file was loaded in
8.4.2.1.3.3 conf()
See
8.4.2.1.3.4 i18n() and format()
See
8.4.2.2 Groovy
When you want to unleash greater power in the templates, you can use groovy
expressions: $g{<groovy-exp>} or
#g{<groovy-exp>}.
e.g. $g{"groovy "*3} prints 'groovy groovy groovy'.
Previous