Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vars docs #168

Merged
merged 5 commits into from
Jul 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
143 changes: 143 additions & 0 deletions docs/tour/vars.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
# Variables & Substitutions

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

```d2
vars: {
server-name: Cat
}

server1: ${server-name}-1
server2: ${server-name}-2

server1 <-> server2
```

<div style={{width: 400}} className="embedSVG" dangerouslySetInnerHTML={{__html: require('@site/static/img/generated/vars-intro.svg2')}}></div>

## Variables can be nested

Use `.` to refer to nested variables.

```d2
vars: {
primaryColors: {
button: {
active: "#4baae5"
border: black
}
}
}

button: {
width: 100
height: 40
style: {
border-radius: 5
fill: ${primaryColors.button.active}
stroke: ${primaryColors.button.border}
}
}
```

<div style={{width: 200}} className="embedSVG" dangerouslySetInnerHTML={{__html: require('@site/static/img/generated/vars-nested.svg2')}}></div>

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

```d2
vars: {
region: Global
font: mono
}

lb: ${region} load balancer
lb.style.font: ${font}

zone1: {
vars: {
region: us-east-1
}
server: ${region} API
server.style.font: ${font}
}
```

<div style={{width: 600}} className="embedSVG" dangerouslySetInnerHTML={{__html: require('@site/static/img/generated/vars-scoped.svg2')}}></div>

## Single quotes bypass substitutions

```d2
vars: {
names: John and Joyce
}
a -> b: 'Send field ${names}'
```

<div className="embedSVG" dangerouslySetInnerHTML={{__html: require('@site/static/img/generated/vars-escaped.svg2')}}></div>

## Spread substitutions

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

```d2
vars: {
base-constraints: [NOT NULL; UNQ]
disclaimer: DISCLAIMER {
I am not a lawyer
near: top-center
}
}

data: {
shape: sql_table
a: int {constraint: [PK; ...${base-constraints}]}
}

custom-disclaimer: DRAFT DISCLAIMER {
...${disclaimer}
}
```

<div style={{width: 400}} className="embedSVG" dangerouslySetInnerHTML={{__html: require('@site/static/img/generated/vars-spread.svg2')}}></div>

## Configuration variables

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

```d2
vars: {
d2-config: {
theme-id: 4
dark-theme-id: 200
pad: 0
center: true
sketch: true
layout-engine: elk
}
}

direction: right
x -> y
```

This is equivalent to calling the following with no `vars`:
```shell
d2 --layout=elk --theme=4 --dark-theme=200 --pad=0 --sketch --center input.d2
```

<div style={{width: 400}} className="embedSVG" dangerouslySetInnerHTML={{__html: require('@site/static/img/generated/vars-config.svg2')}}></div>

:::info Precedence
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.
:::
9 changes: 7 additions & 2 deletions sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ const sidebars = {
"tour/containers",
],
},

{
type: "category",
label: "Special Objects",
Expand Down Expand Up @@ -71,7 +70,13 @@ const sidebars = {
type: "category",
label: "In Depth",
link: { type: "doc", id: "tour/strings" },
items: ["tour/strings", "tour/comments", "tour/overrides", "tour/auto-formatter"],
items: [
"tour/strings",
"tour/vars",
"tour/comments",
"tour/overrides",
"tour/auto-formatter",
],
},
{
type: "category",
Expand Down
13 changes: 13 additions & 0 deletions static/d2/vars-config.d2
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
vars: {
d2-config: {
theme-id: 4
dark-theme-id: 200
pad: 0
center: true
sketch: true
layout-engine: elk
}
}

direction: right
x -> y
5 changes: 5 additions & 0 deletions static/d2/vars-escaped.d2
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
direction: right
vars: {
names: John and Joyce
}
a -> b: 'Send field ${names}'
9 changes: 9 additions & 0 deletions static/d2/vars-intro.d2
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
direction: right
vars: {
server-name: Cat
}

server1: ${server-name}-1
server2: ${server-name}-2

server1 <-> server2
18 changes: 18 additions & 0 deletions static/d2/vars-nested.d2
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
vars: {
primaryColors: {
button: {
active: "#4baae5"
border: black
}
}
}

button: {
width: 100
height: 40
style: {
border-radius: 5
fill: ${primaryColors.button.active}
stroke: ${primaryColors.button.border}
}
}
12 changes: 12 additions & 0 deletions static/d2/vars-scoped.d2
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
vars: {
region: Global
}

lb: ${region} load balancer

zone1: {
vars: {
region: us-east-1
}
server: ${region} API
}
16 changes: 16 additions & 0 deletions static/d2/vars-spread.d2
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
vars: {
base-constraints: [NOT NULL; UNQ]
disclaimer: DISCLAIMER {
I am not a lawyer
near: top-center
}
}

data: {
shape: sql_table
a: int {constraint: [PK; ...${base-constraints}]}
}

custom-disclaimer: DRAFT DISCLAIMER {
...${disclaimer}
}
Loading