Templates
Syntax
The templates engine used in Communications is Thymeleaf, that is widely used framework for templating for html. We will apply it syntax and engine for email html templates and also other textual templates like sms or push notifications. The complete Thymeleaf Doc can be found Here for better understanding of the syntax, including utility objects description that may be very useful for some particular formatting of templates.
Additionally, we will include some basic usage examples that are the one more used in communication Templates.
Strings
| Operation | SMS | |||
| Format | Example | |||
| Input | Template | Output | ||
| Insert variable string | [(${stringVariable})] | name=βPacoβ | Hi [(${name})] ΒΏHow are you? | Hi Paco, ΒΏHow are you? |
| Check if empty | ${#strings.isEmpty(stringVariable)} | name=βjuanβ | Is the name empty? [(${name.isEmpty})] | Is the name empty? false |
| Compare with value | ${#strings.equals(stringVariable,"value")} | name=βpepeβ | Is the name pepe? [(${#strings.equals(name,"pepe")})] | Is the name pepe? true |
| Show only n last characters | [(${#strings.substring(contentCode,#strings.length(contentCode) - n,#strings.length(contentCode))})] | msisdn=β612345678β | We cancel the line ending with [(${#strings.substring(msisdn,#strings.length(msisdn) - 4,#strings.length(msisdn))})] | We cancel the line ending with 5678 |
Numbers
| Operation | SMS | |||
| Format | Example | |||
| Input | Template | Output | ||
| Insert number as it is provided | [(${numberVariable})] | amount=β327,1β | Iβll give you [(${amount})] β¬ | Iβll give you 327,1 β¬ |
| Insert number with n decimals | [(${#numbers.formatDecimal(numberVariable,0,n)})] | amount=β327,1β | Iβll give you [(${#numbers.formatDecimal(amount,0,2)})] β¬ | Iβll give you 327,10 β¬ |
Conditional evaluation
| Operation | SMS | |||
| Format | Example | |||
| Input | Template | Output | ||
| Switch depending on a String variable | [# th:switch="${variableName}"][# th:case="Value1"]Text 1 to include[/][# th:case="Value2")}]Text 2 to include[/][# th:case="*"]Default text to include[/][/] | item=βTVβ | I see [# th:switch="${item}"][# th:case="SIM"]a little SIM[/][# th:case="TV")}]a wonderful television[/][# th:case="*"]an unknown item[/][/] | I see a wonderful television |
| Insert depending of boolean variable | [# th:if=${booleanVariableOrExpression}]Text to include if true[/][# th:unless=${booleanVariableOrExpression}]Text to include if false[/] | boolVar=<true/false> | Is the variable true? [# th:if=${boolVar}]Yes, it is![/][# th:unless="${boolVar}"]No, it is not![/] | If true β Is the variable true? Yes, it is! |
| Insert depending on week day | [# th:switch="${#dates.dayOfWeek(dateVariable)}"][# th:case="1"]Text if Sunday[/][# th:case="2"]Text if Monday[/][# th:case="*"]Text any other day[/][/]Week days number: Sunday β 1, Monday β 2, β¦, Saturday β 7. | dateVariable=23/04/21 (friday) | What day is today? [# th:switch="${#dates.dayOfWeek(dateVariable)}"][# th:case="6"]It's friday!!![/][# th:case=\*"]Not friday yet... :([/][/] | If sent on friday β What day is today? Itβs friday!!! |
| Insert if not empty | [# th:unless=${#strings.isEmpty(stringVariable)}] fixed text + [(${stringVariable})][/] | stringVariable = 666112233 | It is a SIM[# th:unless=${#strings.isEmpty(msisdn)}] linked to [(${msisdn})][/] | It is a SIM linked to 666112233 |