> 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).

# 变量和表达式

借助变量，你可以创建可重复使用的文本，并可在 [表达式](/docs/documentation/zh/chuang-jian-nei-rong/formatting/inline.md#expressions) 并 [自适应内容的条件中](/docs/documentation/zh/fa-bu/adaptive-content/adapting-your-content.md#working-with-the-condition-editor).

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

你可以创建作用范围限定为单个页面或单个空间的变量。

### 创建新变量

要创建新变量，请点击 **资料库** 在编辑一个已打开的页面时，在你的目录中的 [变更请求](/docs/documentation/zh/xie-zuo/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/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="/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/chuang-jian-nei-rong/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.
