gpt4 book ai didi

thymeleaf - 如何使用 Thymeleaf 将标记插入片段

转载 作者:行者123 更新时间:2023-12-02 23:40:42 34 4
gpt4 key购买 nike

我想声明一些布局用作所有表单字段的模板。

大致给出这个片段

<div th:fragment="control(input)" class="form-group">
<label class="col-sm-3 control-label">Edition description</label>
<div class="col-sm-9">
<input th:replace="${input}">
</div>
</div>

这个片段“调用”

<div th:replace=":: control(~{:: input})">
<!--Want to reference this one, not all inputs-->
<input th:field="*{editionDescription}" type="text" class="form-control" placeholder="перераб., дополн.">
</div>

它将产生以下结果:

<div class="form-group">
<label class="col-sm-3 control-label">Edition description</label>
<div class="col-sm-9">
<input th:field="*{editionDescription}" type="text" class="form-control" placeholder="перераб., дополн.">
</div>
</div>

问题是 :: input输入选择器应用于所有 <input>当前处理的模板中的标签。如何仅引用随附的一份?

此外,您能否就如何解决这个问题提出建议。我想尽可能地模仿 Grails Fields 插件。你认为我最好声明我自己的方言吗?

最佳答案

您应该为您的输入元素定义类(或标识符)(您想要替换的元素),然后您可以使用适当的选择器语法只是为了选择所需的输入。您可以在 here 中找到选择器语法详细信息

示例代码可能如下所示:

<div th:fragment="control(input)" class="form-group">
<label class="col-sm-3 control-label">Edition description</label>
<div class="col-sm-9">
<input th:replace="${input}">
</div>
</div>

那么你的选择器应该是这样的:

<div th:replace=":: control(~{:: input.description})">
<!--Want to reference this one, not all inputs-->
<input th:field="*{editionDescription}" type="text" class="form-control description" placeholder="перераб., дополн.">
</div>

请注意使用 ::input.description 作为带有类标识符的选择器

关于thymeleaf - 如何使用 Thymeleaf 将标记插入片段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41350936/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com