是否可以将片段传递给Thymeleaf中的消息表达式?我想重新使用片段在消息中创建链接.
我的片段看起来像这样:
<div th:fragment="link(url, text)" th:remove="tag">
<a th:href="@{${url}}"><span>${text}</span></a>
</div>
Run Code Online (Sandbox Code Playgroud)
我有这样的信息:
home.welcome=Hello User! See new content at {0}.
Run Code Online (Sandbox Code Playgroud)
现在我想将评估的片段传递给消息表达式(伪代码):
<p th:utext="#{home.welcome(${link:: link(url='myUrl', text='myText')})}"></p>
Run Code Online (Sandbox Code Playgroud)
生成的HTML应如下所示:
<p>
Hello User! See new content at <a href="myUrl"><span>myText</span></a>.
</p>
Run Code Online (Sandbox Code Playgroud)
我发现在Thymeleaf 3中引入了Fragment表达式,但我不确定它们是否可行.
Abd*_*han -1
您可以尝试th:with。
像这样调用片段
<div th:include="fragments/common :: link" th:with="url='www.google.com', text='Click Me'"></div>
Run Code Online (Sandbox Code Playgroud)
这是你的片段
<div th:fragment="link" th:remove="tag">
<a th:href="@{${url}}"><span th:inline="text">[[${text}]]</span></a>
</div>
Run Code Online (Sandbox Code Playgroud)
这是您将得到的 HTML
<div>
<a href="www.google.com">
<span>Click Me</span>
</a>
</div>
Run Code Online (Sandbox Code Playgroud)