Variables and templating
Variables
You can declare a variable in one of the following ways:
- pass it in the settings when building documentation;
- describe it in the variable presets file.
Below are the options for using variables in a document.
Substitutions
To substitute a variable value into text, enclose its name on both sides with double curly braces.
Какой-то текст {{ имя_переменной }} продолжение текста.
If the text contains double curly braces but does not imply variable substitution, add not_var before the construct.
Какой-то текст not_var{{ тоже_текст }} продолжение текста.
Conditional operators
Warning
To use conditional operators, set the conditionsInCode parameter to true in the .yfm configuration file.
You can use the conditional operators if, else, and elsif to include specific text fragments in a document depending on variable values. For example, to build two versions of a document for different operating systems.
{% if OS == 'iOS' %}
Скачайте приложение в [App Store](https://www.apple.com/ios/app-store/).
{% else %}
Скачайте приложение в [Google Play](https://play.google.com).
{% endif %}
Conditional operators can be applied not only to text blocks but also to text fragments within lines.
Какой-то текст {% if OS == 'iOS' %} Apple {% else %} Android {% endif %} продолжение текста.
Supported constructs
Comparison operators: == , != , > , < , >= , <=
Logical operators: and , or
The contains operator:
- for string A, checks that it contains substring B
{% if Object.title contains 'API' %} - for array A, checks that it contains element B
{% if keywords contains 'Extension' %}
Loops
Use loops to output repeated content for each element of an array. Inside a loop, refer to an element as a regular variable using the syntax for substitution.
{% for имя_переменной in имя_массива %}
Какой-то текст {{ имя_переменной }} продолжение текста.
{% endfor %}
Examples of using loops
Suppose the variable presets file defines an array users:
default:
users:
- Alice
- Mark
Then using loops will produce the following results:
Prefix {% for user in users %} {{user}} {% endfor %} Postfix
Prefix Alice Mark Postfix
Prefix
{% for user in users %}
{{user}}
{% endfor %}
Postfix
Prefix
Alice
Mark
Postfix
Filters
To apply a filter, add the operator | and the filter name to the variable. The operator is separated by spaces on both sides.
| Filter | Description |
|---|---|
capitalize |
Converts the first letter found in the variable value to uppercase. |
length |
Calculates the length of the variable value. |
Examples of using filters
Suppose the variable presets file defines:
default:
user:
name: alice
users:
- Alice
- Mark
Then using filters will produce the following results:
Hello {{ user.name | capitalize }}!
Hello Alice!
{{ users | length }}
{{ user.name | length }} | length
2
5
Functions
To call a function, add the character . to the variable, specify its name, and pass the required parameters in parentheses ().
The function slice(beginIndex, endIndex) returns the specified part of the original array as a new array object.
Parameters:
beginIndex— the index of the element from which the extraction starts (numbering starts at 0).endIndex— the index of the element at which the extraction ends (numbering starts at 0).
If the parameter is not specified, all elements from the starting position to the end of the array are selected.
Examples of using functions
Let the following be set in the variable presets file:
default:
user:
name: Masha
Then using the functions will lead to the following results:
Hello P{{ user.name.slice(1) }}!
Hello P{{ user.name.slice(1, 2) }}vel!
Hello Pasha!
Hello Pavel!