Thymeleaf有三种模板模式,分别是TEXT,JAVASCRIPT和CSS
1. TEXT
TEXT中使用的语法方式为: [(${变量})]
一般变量
您好, [(${name})],您要购买的是 [(${report.name})] 吗?
循环
[# th:each="user: ${users}"]
用户名称为: [(${user.name})]
[/]
完整示例
Dear [(${customer.name})],
This is the list of our products:
[# th:each="prod : ${products}"]
- [(${prod.name})]. Price: [(${prod.price})] EUR/kg
[/]
Thanks,
The Thymeleaf Shop
执行后,结果可能类似于:
Dear Mary Ann Blueberry,
This is the list of our products:
- Apricots. Price: 1.12 EUR/kg
- Bananas. Price: 1.78 EUR/kg
- Apples. Price: 0.85 EUR/kg
- Watermelon. Price: 1.91 EUR/kg
Thanks,
The Thymeleaf Shop
2. JAVASCRIPT与CSS
JAVASCRIPT中使用的语法方式为(CSS类似): [[${变量}]]
示例
var greeter = function() {
var username = [[${session.user.name}]];
[# th:each="salut : ${salutations}"]
alert([[${salut}]] + " " + username);
[/]
};
执行后,结果可能类似于:
var greeter = function() {
var username = "Bertrand \"Crunchy\" Pear";
alert("Hello" + " " + username);
alert("Ol\u00E1" + " " + username);
alert("Hola" + " " + username);
};