# 变量和表达式

使用变量，你可以创建可重复使用的文本，这些文本可以在 [表达式](/docs/documentation/zh/creating-content/formatting/inline.md#expressions) 和 [自适应内容的条件中](/docs/documentation/zh/zhan-dian-fang-wen/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` 对象访问。

<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: 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://gitbook.com/docs/documentation/zh/creating-content/variables-and-expressions.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.
