> For the complete documentation index, see [llms.txt](https://gitbook.com/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://gitbook.com/docs/documentation/zh/creating-content/variables-and-expressions.md).

# 变量与表达式

使用变量，你可以创建可复用的文本，这些文本可以在 [表达式](/docs/documentation/zh/creating-content/formatting/inline.md#expressions) 以及 [自适应内容的条件](/docs/documentation/zh/zhan-dian-fang-wen-quan-xian/adaptive-content/adapting-your-content.md#working-with-the-condition-editor).

如果你在内容中多次重复相同的名称、短语或版本号，你可以创建一个 **变量** 来帮助保持所有这些实例同步且准确——这在你需要更新它们，或者它们很复杂且经常被误输入时很有用。

你可以创建作用域限定为单个页面或单个章节的变量。

### 创建新变量

要创建新变量，请点击 **资源库** 在编辑已打开的 [变更请求](/docs/documentation/zh/collaboration/change-requests.md)时的目录中。然后，点击 **变量**.

你可以使用顶部的切换开关，查看并创建仅适用于你当前所在页面，或当前章节内所有页面的变量。

点击 **创建变量** 将会打开一个模态窗口，你可以为变量命名并赋值。

点击 **添加变量** 以保存你的变量。

<figure><img src="/files/cc6d4f3771aad0927e4468738f15c5dfb8cba040" alt="A GitBook screenshot showing the Add variables screen. The variable Name box has been filled with the text ‘latest_version’ and the Value box has been filled with the text ‘v3.04.1’"><figcaption><p>你可以将变量添加到单个页面或整个章节中。当你更新变量的值时，它的每个实例都会随之更新。</p></figcaption></figure>

{% hint style="info" %}
变量名称必须以字母开头，并且可以包含字母、数字和下划线。
{% endhint %}

### 在内容中使用变量

变量可以在一个 [表达式](/docs/documentation/zh/creating-content/formatting/inline.md#expressions) 表达式中被引用和使用——你可以将其内联插入到内容中。插入表达式后，双击它即可打开表达式编辑器。

{% hint style="info" %}
表达式只能用于文档的页面内容中。它们不能用于页面标题或页面元数据。
{% endhint %}

在页面下定义的变量可在 `page.vars` 对象下访问。同样，在整个章节范围内定义的变量可在 `space.vars` 对象下访问。

{% hint style="info" %}
在表达式中，章节范围的变量位于 `space.vars` —— GitBook API 将章节表示为 `space` 对象。
{% endhint %}

<figure><img src="/files/fa14f5ad70d98845bcc22d2f52ce414a9fe28622" alt="A GitBook screenshot showing an expression block within the editor. The expression editor is open below it and the ‘space.vars.latest_version’ variable has been selected"><figcaption><p>你可以通过表达式将变量添加到内容中。表达式编辑器提供自动补全选项，帮助你找到所需的变量。</p></figcaption></figure>

### 更新变量

你可以在变更请求中随时更新变量。更新其值会同步更新所有引用它的表达式块中的值。一旦变更请求合并，已更改的变量就会在任何已发布的网站上生效。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://gitbook.com/docs/documentation/zh/creating-content/variables-and-expressions.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
