> 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/chuang-jian-nei-rong/variables-and-expressions.md).

# 变量与表达式

创建可重复使用的变量，这些变量可在页面和分区中引用

借助变量，您可以创建可在以下内容中有条件地引用的可复用文本： [表达式](https://gitbook.com/docs/documentation/zh/chuang-jian-nei-rong/formatting/inline.md#expressions) 和 [自适应内容条件](https://gitbook.com/docs/documentation/zh/publish/adaptive-content/adapting-your-content.md#working-with-the-condition-editor).

如果您在内容中多次重复使用同一个名称、短语或版本号，可以创建一个 **变量** 来帮助确保所有这些实例保持同步且准确无误——当您需要更新它们，或它们较复杂、容易拼错时，这会很有用。

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

### 创建新变量

要创建新变量，请点击 **库** 编辑已打开的 [变更请求](https://gitbook.com/docs/documentation/zh/collaborate/change-requests.md)。然后，点击 **变量**.

您可以使用顶部的切换按钮查看并创建作用范围仅限于当前页面或当前章节内所有页面的变量。

点击 **创建变量** 会打开一个模态窗口，您可以在其中为变量设置名称和值。

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

<figure><img src="https://2111890564-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNkEGS7hzeqa35sMXQZ4X%2Fuploads%2Fgit-blob-c8a49c22f14f326ac8f05fd190051c8a5dd0c6bd%2F25_12_10_add_variable%402x.png?alt=media" 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 %}

### 在内容中使用变量

变量可以在 [表达式](https://gitbook.com/docs/documentation/zh/chuang-jian-nei-rong/formatting/inline.md#expressions) 中引用和使用 — 表达式可以内嵌到内容中。插入表达式后，双击它即可打开表达式编辑器。

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

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

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

<figure><img src="https://2111890564-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNkEGS7hzeqa35sMXQZ4X%2Fuploads%2Fgit-blob-8e364840ff56aaf06150756001d44c2cf1292410%2F25_12_10_add_variable%402x_1.png?alt=media" 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 by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://gitbook.com/docs/documentation/zh/chuang-jian-nei-rong/variables-and-expressions.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
