java Thymeleaf - 包括片段的内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35988081/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Thymeleaf - Include content of fragment
提问by kidwon
Thymeleaf fragment:
百里香片段:
<div th:fragment="assets">
<script src="myscript"></script>
<script src="myscript2"></script>
</div>
This piece of code inserts the fragment:
这段代码插入片段:
<div th:replace="fragments/assets :: assets"></div>
How to include only content without the wrapper?
如何只包含没有包装器的内容?
<script src="myscript"></script>
<script src="myscript2"></script>
采纳答案by Balaji Krishnan
try the below
试试下面的
<div th:fragment="assets" th:remove="tag">
<script src="myscript"></script>
<script src="myscript2"></script>
</div>
回答by Dhanendra Kumar
You can use th:block to include only content of a block.
您可以使用 th:block 仅包含块的内容。
Define your fragment like -
像这样定义你的片段 -
<th:block th:fragment="assets">
<script src="myscript"></script>
<script src="myscript2"></script>
</th:block>
And include like this -
并包括这样 -
<th:block th:include="fragments/assets :: assets"></th:block>
Hope this will help you :)
希望能帮到你 :)
回答by juanlumn
Yout Thymeleaf fragment:
Yout Thymeleaf 片段:
<div th:fragment="assets" th:remove="tag">
<script src="myscript"></script>
<script src="myscript2"></script>
</div>
Your code insert of the fragment:
您的片段代码插入:
<div th:replace="fragments/assets :: assets"></div>
This should render like:
这应该呈现如下:
<script src="myscript"></script>
<script src="myscript2"></script>
回答by Sachin Kumar
Use this:
用这个:
<div id="testId" th:include="fragments/assets :: assets"></div>
It will also insert the specified fragment as the body of its with in outer tag but excluding the fragment tag.
它还会将指定的片段作为其正文插入到外部标签中,但不包括片段标签。
回答by Dave Bower
The best way I've found to handle the script tag case is to use Thymeleaf Layoutssince fragments are always nested inside a tag when using th:include or th:replace.
我发现处理脚本标签情况的最好方法是使用Thymeleaf 布局,因为在使用 th:include 或 th:replace 时,片段总是嵌套在标签内。
Further, one useful approach I use is to add the common scripts as normal at the bottom of a layout template and then add a final script content block e.g:
此外,我使用的一种有用方法是在布局模板的底部正常添加通用脚本,然后添加最终脚本内容块,例如:
...
<script type="text/javascript" src="/resources/js/jquery-1.11.1.min.js"></script>
<script type="text/javascript" src="/resources/js/bootstrap.min.js"></script>
<div layout:fragment="script"></div>
</body>
</html>
Then pages which use the template can specify any extra javascript in a "script" block:
然后使用模板的页面可以在“脚本”块中指定任何额外的 javascript:
<script th:inline="javascript" layout:fragment="script" type="text/javascript">
/*<![CDATA[*/
// Some javascript...
/*]]>*/