# 片段的高级条件插入

空片段和无操作标记允许我们以一种非常简单优雅的方式执行片段的条件插入。

例如，为了只在用户是管理员的时候，才插入我们的`common :: adminhead`片段。不是管理员的时候，不插入（空片段）。我们可以这样写：

```markup
...
<div th:insert="${user.isAdmin()} ? ~{common :: adminhead} : ~{}">...</div>
...
```

另外，为了只在特定条件满足时插入片段，在条件不满足时不修改标记，我们可以使用无操作标记：

```markup
...
<div th:insert="${user.isAdmin()} ? ~{common :: adminhead} : _">
    Welcome [[${user.name}]], click <a th:href="@{/support}">here</a> for help-desk support.
</div>
...
```

此外，如果我们已经配置好模版解析器检查模版资源是否存在——依据`checkExistence`标记，我们可以把片段自身存在与否用作默认操作的条件：

```markup
...
<!-- The body of the <div> will be used if the "common :: salutation" fragment  -->
<!-- does not exist (or is empty).                                              -->
<div th:insert="~{common :: salutation} ?: _">
    Welcome [[${user.name}]], click <a th:href="@{/support}">here</a> for help-desk support.
</div>
...
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://jack80342.gitbook.io/thymeleaf/i.-using-thymeleaf/8-mo-ban-she-ji/8.3-flexible-layouts-beyond-mere-fragment-insertion/advanced-conditional-insertion-of-fragments.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
