是否可以将片段传递到Thymeleaf中的消息表达式?我想重复使用片段在消息中创建链接。
我的片段看起来像这样:
<div th:fragment="link(url, text)" th:remove="tag">
<a th:href="@{${url}}"><span>${text}</span></a>
</div>
我有这样的消息:
home.welcome=Hello User! See new content at {0}.
现在,我想将评估后的片段传递给消息表达式(伪代码):
<p th:utext="#{home.welcome(${link:: link(url='myUrl', text='myText')})}"></p>
产生的HTML应该如下所示:
<p>
Hello User! See new content at <a href="myUrl"><span>myText</span></a>.
</p>
我发现Thymeleaf 3中引入了Fragment expressions,但是我不确定它们是否可行。
最佳答案
您可以使用尝试 th:。
像这样调用片段<div th:include="fragments/common :: link" th:with="url='www.google.com', text='Click Me'"></div>
这是你的片段<div th:fragment="link" th:remove="tag">
<a th:href="@{${url}}"><span th:inline="text">[[${text}]]</span></a>
</div>
这是您将获得的HTML<div>
<a href="www.google.com">
<span>Click Me</span>
</a>
</div>
关于thymeleaf - 如何将片段传递给Thymeleaf中的消息表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47023365/