# 変数と式

変数を使うと、条件付きで参照できる再利用可能なテキストを [式](https://gitbook-open-v2-preview.gitbook.workers.dev/url/gitbook.com/docs/documentation/ja-gitbook-documentation/formatting/inline#expressions) 、 [適応コンテンツの条件](https://gitbook-open-v2-preview.gitbook.workers.dev/url/gitbook.com/docs/documentation/ja-gitbook-documentation/publishing-documentation/adaptive-content/adapting-your-content#working-with-the-condition-editor).

同じ名前、語句、またはバージョン番号をコンテンツ内で何度も繰り返す場合、 **変数** を作成して、それらのすべての出現箇所を同期させ、正確に保つことができます。更新が必要になった場合や、複雑で入力ミスが多い場合に便利です。

変数は、1つのページ単位または1つのスペース単位でスコープを設定して作成できます。

### 新しい変数を作成

新しい変数を作成するには、 **ライブラリ** をクリックします。これは、開いている [変更リクエスト](https://gitbook-open-v2-preview.gitbook.workers.dev/url/gitbook.com/docs/documentation/ja-gitbook-documentation/collaboration/change-requests)を編集しているときの目次内にあります。次に、 **変数**.

上部の切り替えを使って、現在のページに限定した変数、または現在のスペース内のすべてのページに対する変数を表示・作成できます。

をクリックすると **変数を作成** モーダルが開き、変数に名前と値を設定できます。

クリック **変数を追加** をクリックして変数を保存します。

<figure><img src="https://4217681718-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/ja-gitbook-documentation/formatting/inline#expressions) の中で参照して使用できます。これはコンテンツ内にインラインで挿入できます。式を挿入した後、ダブルクリックして式エディターを開きます。

ページの下で定義された変数は、 `page.vars` オブジェクトから参照できます。同様に、スペース全体で定義された変数は、 `space.vars` オブジェクト。

<figure><img src="https://4217681718-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>

### 変数を更新する

変更リクエストの中であれば、いつでも変数を更新できます。値を更新すると、それを参照しているすべての式ブロックにわたって値が更新されます。変更された変数は、変更リクエストがマージされると、公開済みのサイトに反映されます。
