Variables
Variables let you customize your Markdoc documents at runtime.
Here I am rendering a custom {% $variable %}
You can pass variables in a few ways:
- Through the
variablesfield on yourConfig - Via the
variablesattribute on apartialtag. - Manually from within your
NodeorTagtransformfunctions.
Global variables
Here's an example of how you can pass variables to your config:
const doc = `
{% if $flags.my_feature_flag %}
Username: {% $user.name %}
{% /if %}
`;
const config = {
variables: {
flags: {
my_feature_flag: true
},
user: {
name: 'Dr. Mark'
}
}
};
const ast = Markdoc.parse(doc);
const content = Markdoc.transform(ast, config);
which you can then access within your document:
{% if $flags.my_feature_flag %}
Username: {% $user.name %}
{% /if %}
With partials
To pass variables to a partial, set the variables attribute:
{% partial variables={sdk: "Ruby", version: 3} file="header.md" /%}
and access the value within your partial file the same way you would a regular variable:
SDK: {% $sdk %}
Version: {% $version %}