---
title: Reusable snippets and variables
subtitle: Write repeated content once and reference it everywhere — snippet files, parameterized snippets, page variables, and conditional content.
description: Reusable snippets in ButterIX (the Markdown / Snippet / Include components and import), snippet parameter placeholders, page-level export const variables, and showing content by locale, version, or product with the If component.
---

export const cliVersion = "0.1.0"
export const minNode = "22.13"

## Snippet files

A snippet is just a regular `.mdx` / `.md` file that can contain any Markdown and components. By convention they live in the `snippets/` directory at the project root. Here's this site's `snippets/install-cli.mdx`:

<Code src="/snippets/install-cli.mdx" title="snippets/install-cli.mdx" />

### Reference with a component

```mdx
<Markdown src="/snippets/install-cli.mdx" />
```

<Markdown src="/snippets/install-cli.mdx" />

`<Snippet>` and `<Include>` are aliases of `<Markdown>`; the path prop can also be written as `file` or `path`. Path resolution rules:

| Syntax | Resolves to |
|---|---|
| `/snippets/x.mdx` (or `@/snippets/x.mdx`) | A path under the project root |
| `./x.mdx`, `../shared/x.mdx` | Relative to the current file |
| `x.mdx` | `snippets/x.mdx` at the project root |

### Reference with import

You can also import a snippet and use it as a component:

```mdx
import InstallCli from "/snippets/install-cli.mdx"

<InstallCli />
```

Only `.md` / `.mdx` files can be imported, and only default imports are supported; importing a JavaScript module reports an `mdx/import` error. Snippets can reference other snippets in turn (up to 5 levels deep).

## Parameterized snippets

Extra props passed to a snippet replace placeholders in the snippet body. Since `{…}` is an expression in MDX, **placeholders in snippets must be escaped as `\{name\}`**:

<Code src="/snippets/node-requirement.mdx" title="snippets/node-requirement.mdx" />

```mdx
<Markdown src="node-requirement.mdx" node="22.13" cli="0.1.0" />
```

<Markdown src="node-requirement.mdx" node="22.13" cli="0.1.0" />

Placeholders without a passed-in value are shown as-is.

<Warning>
  If you write an unescaped `{node}` directly in a snippet, MDX evaluates it as an expression; `node` isn't a known variable, so the expression is dropped with an `mdx/expression` warning, and no replacement can happen.
</Warning>

## Page variables

Declare literals with `export const` at the top of the page, then use them in the body or in component props:

```mdx
export const cliVersion = "0.1.0"
export const minNode = "22.13"

The current CLI version is {cliVersion} and requires Node.js {minNode} or later.

<Badge intent="success">{"v" + cliVersion}</Badge>
```

The current CLI version is {cliVersion} and requires Node.js {minNode} or later.

<Badge intent="success">{"v" + cliVersion}</Badge>

Variable values can only be literals: strings, numbers, booleans, arrays, plain objects, template strings, plus `+` concatenation between them, ternary expressions, and member access (such as `{plans.pro.price}`). Functions, classes, regular expressions, and any calls are not allowed; see [Safe subset](./safe-mdx.mdx).

## Conditional content

`<If>` (alias `<Conditional>`) decides whether to render content based on the locale, version, or product of the current page, without producing a wrapper element. It's especially useful when a snippet is shared across locales or versions:

```mdx
<If locales={["zh"]}>
  This text only appears on Chinese pages.
</If>

<If versions={["v0"]}>
  You're reading the legacy docs.
</If>

<If versions={["v0"]} not>
  This only appears outside v0.
</If>
```

<If locales={["zh"]}>
  <Check>你正在阅读中文版，这个提示由 `<If locales={["zh"]}>` 控制。</Check>
</If>

<If locales={["en"]}>
  <Check>You are reading the English version.</Check>
</If>

<ParamField path="locales" type="string[]">Locale codes, e.g. `["en", "zh"]`.</ParamField>
<ParamField path="versions" type="string[]">Version ids, slugs, or display names.</ParamField>
<ParamField path="products" type="string[]">Product ids, slugs, or display names.</ParamField>
<ParamField path="not" type="boolean">Negate the condition.</ParamField>
<ParamField path="roles" type="string[]">Show content by reader role. Static sites have no signed-in users, so conditions with `roles` never match.</ParamField>

## Snippets and translations

Snippet files aren't split by locale: Chinese and English pages reference the same file. When you need multiple locales, there are two approaches:

1. Keep locale-specific text in the pages, and put only locale-independent content such as code in snippets (like this site's `install-cli.mdx`).
2. Wrap paragraphs for different locales in `<If locales>` inside the snippet.
