> 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="https://2111890564-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNkEGS7hzeqa35sMXQZ4X%2Fuploads%2FQwfTEGjsxDThqQFvgOm8%2FAdd%20variable%402x.png?alt=media&amp;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 %}

### 在内容中使用变量

变量可以在……中被引用和使用 [表达式](/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%2FaRWyFWMI24Cy2nVZmSzE%2FUsing%20variable%402x.png?alt=media&amp;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>

### 更新变量

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


---

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