Skip to main content

Variables & Substitutions

vars is a special keyword that lets you define variables. These variables can be referenced with the substitution syntax: ${}.

Cat-1Cat-2

Variables can be nested​

Use . to refer to nested variables.

button

Variables are scoped​

They work just like variable scopes in programming. Substitutions can refer to variables defined in a more outer scope, but not a more inner scope. If a variable appears in two scopes, the one closer to the substitution is used.

Global load balancerzone1us-east-1 API

Single quotes bypass substitutions​

ab Send field ${names}

Spread substitutions​

If x is a map or an array, ...${x} will spread the contents of x into either a map or an array.

dataaintPK, NOT NULL, UNQDRAFT DISCLAIMERI am not a lawyer

Configuration variables​

Some configurations can be made directly in vars instead of using flags or environment variables.

This is equivalent to calling the following with no vars:

xy
Precedence

For top-level settings such as theme-id and pad, flags and environment variables take precedence.

In other words, if you call D2_PAD=2 d2 --theme=1 input.d2, it doesn't matter what theme-id and pad are set to in input.d2's d2-config; it will use the options from the command.

animate-interval sets the playback interval for all boards. animate-duration sets the transition time for steps. It also controls pencil shading and text decorations between layers and scenarios. See the animation configuration. Use animate-progress and animate-progress-color to control the playback progress line.

Output scale and SVG appendix​

On D2's development branch (master), you can set scale and force-appendix in the diagram. These source options are available in D2 v0.10.0 and later.

scale: 0.5 renders at half size. Use -1 for automatic sizing, which lets an SVG fit its container. force-appendix: true adds numbered notes for links and tooltips to SVG exports, including animated boards. PNG exports already include the notes. To export the same diagram at its natural size without the SVG appendix, run d2 --scale=1 --force-appendix=false input.d2 output.svg.

data

data holds key-value pairs that applications embedding D2 can read. Starting in v0.10.0, you can also set Dagre and ELK layout options here. These values take precedence over the corresponding CLI flags. This example sets the space between adjacent ELK layers to 100 pixels.