如何使用 thymeleaf 在 bootstrap 标签成功中插入变量中的字符串
How can I interpolate a string from varaible in a bootstrap label-success with thymeleaf
我有以下html内容:
<span th:if="${game.isWon()}" class="label label-success">
YOU WIN! Game Score: ${game.getGameScore()}.</span>
我不知道如何插入 game.getGameScore() 并且原始字符串不断被渲染。我将 thymeleaf 与 Spring Boot 一起使用。
任何帮助将不胜感激。
如果您想直接在文本中使用属性(而不是在 HTML 属性中),您必须使用具有自己语法的 inlining。 (请注意,内联在 Thymeleaf 3 中默认启用,但您可能必须在早期版本中使用属性 th:inline="text"
)。例如:
<span th:if="${game.won}" class="label label-success">
YOU WIN! Game Score: [[${game.gameScore}]].
</span>
传统的方法是添加一些额外的标签:
<span th:if="${game.won}" class="label label-success">
YOU WIN! Game Score: <span th:text="${game.gameScore}" />.
</span>
假设您有一个游戏 bean,使用字段 won
和 gameScore
以及相关的 getter:
public class Game {
private boolean won;
private int gameScore;
public boolean isWon() {
return won;
}
public void setWon(boolean won) {
this.won = won;
}
public int isGameScore() {
return gameScore;
}
public void setGameScore(int gameScore) {
this.gameScore = gameScore;
}
}
那么你可以使用这个:
<span th:if="${game.won}"
th:text="'YOU WIN! Game Score: ' + ${game.gameScore} + '.'"
class="label label-success">
</span>
这会生成以下 HTML:
<span class="label label-success">YOU WIN! Game Score: 123.</span>
我有以下html内容:
<span th:if="${game.isWon()}" class="label label-success">
YOU WIN! Game Score: ${game.getGameScore()}.</span>
我不知道如何插入 game.getGameScore() 并且原始字符串不断被渲染。我将 thymeleaf 与 Spring Boot 一起使用。 任何帮助将不胜感激。
如果您想直接在文本中使用属性(而不是在 HTML 属性中),您必须使用具有自己语法的 inlining。 (请注意,内联在 Thymeleaf 3 中默认启用,但您可能必须在早期版本中使用属性 th:inline="text"
)。例如:
<span th:if="${game.won}" class="label label-success">
YOU WIN! Game Score: [[${game.gameScore}]].
</span>
传统的方法是添加一些额外的标签:
<span th:if="${game.won}" class="label label-success">
YOU WIN! Game Score: <span th:text="${game.gameScore}" />.
</span>
假设您有一个游戏 bean,使用字段 won
和 gameScore
以及相关的 getter:
public class Game {
private boolean won;
private int gameScore;
public boolean isWon() {
return won;
}
public void setWon(boolean won) {
this.won = won;
}
public int isGameScore() {
return gameScore;
}
public void setGameScore(int gameScore) {
this.gameScore = gameScore;
}
}
那么你可以使用这个:
<span th:if="${game.won}"
th:text="'YOU WIN! Game Score: ' + ${game.gameScore} + '.'"
class="label label-success">
</span>
这会生成以下 HTML:
<span class="label label-success">YOU WIN! Game Score: 123.</span>