# 变量与表达式

使用变量，您可以创建可重复使用的文本，并可在 [表达式](https://gitbook-open-v2-preview.gitbook.workers.dev/url/gitbook.com/docs/documentation/zh/formatting/inline#expressions) 并在 [自适应内容的条件](https://gitbook-open-v2-preview.gitbook.workers.dev/url/gitbook.com/docs/documentation/zh/publishing-documentation/adaptive-content/adapting-your-content#working-with-the-condition-editor).

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

您可以创建仅作用于单个页面或单个空间的变量。

### 创建新变量

要创建新变量，请单击 **资源库** 在编辑已打开的 [变更请求](https://gitbook-open-v2-preview.gitbook.workers.dev/url/gitbook.com/docs/documentation/zh/collaboration/change-requests)时位于目录中的。然后，单击 **变量**.

您可以使用顶部的切换开关，查看并创建仅作用于当前所在页面或当前空间内所有页面的变量。

单击 **创建变量** 将打开一个模态窗口，您可以在其中为变量命名并设置值。

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

<figure><img src="https://2111890564-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNkEGS7hzeqa35sMXQZ4X%2Fuploads%2FQwfTEGjsxDThqQFvgOm8%2FAdd%20variable%402x.png?alt=media&#x26;token=955216f0-67df-4c96-aece-490cfbbf0271" 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-open-v2-preview.gitbook.workers.dev/url/gitbook.com/docs/documentation/zh/formatting/inline#expressions) 中被引用和使用——您可以将其内联插入内容中。插入表达式后，双击它以打开表达式编辑器。

在您的页面下定义的变量可通过 `page.vars` 对象访问。同样，在整个空间中定义的变量可通过 `space.vars` 对象来定义。

<figure><img src="https://2111890564-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNkEGS7hzeqa35sMXQZ4X%2Fuploads%2FaRWyFWMI24Cy2nVZmSzE%2FUsing%20variable%402x.png?alt=media&#x26;token=a1999774-f0da-4232-8533-b7e806ddd87c" 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>

### 更新变量

在变更请求中，您可以在任何时候更新变量。更新其值将同步更新引用它的任何表达式块中的值。一旦变更请求合并，已更改的变量将对任何已发布的网站生效。
