What does {{ }} mean in YAML?
Last Updated : 10 Sep, 2024
YAML (YAML Ain't Markup Language) is a popular human-readable data serialization format often used for configuration files and data exchange. YAML's simplicity and readability have made it a favorite among developers and system administrators. While YAML is generally straightforward, there are times when you encounter unfamiliar constructs, such as `{{ }}`. In this article, we'll delve into what `{{ }}` means in YAML, provide examples, and cover every aspect of its usage.
YAML Basics
Before diving into {{ }}, let's briefly recap some YAML basics.
- YAML uses indentation to define the structure of data.
- It supports key-value pairs, lists, and nested structures.
- Comments start with the `#` character and are used for explanatory notes.
- YAML is whitespace-sensitive, meaning that proper indentation is crucial.
Understanding `{{ }}` in YAML
The `{{ }}` construct in YAML is used to denote a "tagged scalar." It's a way to specify that a string should be treated as a specific data type or to apply a transformation to it. Essentially, `{{ }}` allows you to assign additional metadata or behaviors to a value within a YAML document.
Examples of `{{ }}`
Let's explore some common use cases for {{ }} in YAML with examples:
Example 1: Tagging Scalars
age: !!int 20
In this example, we have a YAML key-value pair where the key is "age," and the value is `!!int 20`.
- `!!int` inside `{{ }}` is a YAML tag that tells the YAML parser to interpret the value as an integer.
- The `20` is a scalar value (a plain number) without any quotes or special notation.
When this YAML document is parsed, the YAML parser will recognize that the `age` value is tagged as an integer (`!!int`), ensuring that it's treated as a numeric data type rather than a string. This can be useful when you want to maintain the data type's integrity.
Example 2: Applying Transformations
name: !!str {{ name.toUpperCase() }}
In this example, we have a YAML key-value pair where the key is "name," and the value is `{{ name.toUpperCase() }}`.
- `{{ }}` is used to apply a transformation to the value of `name`.
- name.toUpperCase() is a JavaScript-like expression enclosed within `{{ }}`. It transforms the `name` variable to uppercase.
When the YAML document is processed, it will evaluate the expression `name.toUpperCase()` and assign the result (the uppercase version of the `name` variable) as the value for the "name" key.
This is a powerful way to modify or preprocess data within YAML files, especially when dealing with configuration files or data transformations.
Example 3: Environment Variable Substitution
database_url: {{ env('DB_URL') }}
In this example, we have a YAML key-value pair where the key is "database_url," and the value is`{{ env('DB_URL') }}`.
- `{{ env('DB_URL') }}` is a common use case for `{{ }}` in configuration files. It's used to substitute the value with the contents of the `DB_URL` environment variable.
When the YAML document is processed, the `{{ env('DB_URL') }}` expression is replaced with the actual value of the `DB_URL` environment variable, effectively setting the "database_url" key to the database URL stored in the environment variable.
This is a valuable feature for injecting dynamic or sensitive data into configuration files without hardcoding it, making the configuration more flexible and secure.
Nested `{{ }}`
value: !!int {{ env('SOME_ENV_VARIABLE').toUpperCase() }}
In this example, we have a YAML key-value pair where the key is "value," and the value is a combination of nested {{ }} constructs.
- `{{ env('SOME_ENV_VARIABLE') }}` retrieves the value of the "SOME_ENV_VARIABLE" environment variable.
- `.toUpperCase()` is applied to the result of `{{ env('SOME_ENV_VARIABLE') }}` to convert it to uppercase.
- Finally, `!!int` tags the entire expression as an integer.
When processed, this YAML document will:
- Retrieve the value of the "SOME_ENV_VARIABLE" environment variable.
- Convert it to uppercase.
- Treat the result as an integer.
The resulting integer value is then assigned to the "value" key.
This example illustrates the flexibility of `{{ }}` in allowing you to perform multiple operations on a value within a YAML document.
Conclusion
In this artcile we disscude `{{ }}` in YAML which is a versatile feature that can be used to tag values with data types, apply transformations, and perform dynamic substitutions, enhancing the flexibility and functionality of YAML documents, especially in configuration and data management contexts.
Similar Reads
What Does $ Mean in Python? The $ operator in Python is used for string formatting with the Template class. It acts as a placeholder or substitute indicator for variables within a string, similar to Python's f-strings.Using $ with substitute()In this example, we will see how $ in Python string acts as a placeholder. Here, we a
1 min read
What does '...' mean in JavaScript? The '...' (or 3 dot symbol) in JavaScript is known as the Spread operator or Rest operator based on the usage. This syntax is used to expand the iterable into individual elements such as arrays, objects, etc.Syntax for Spread Operatorcosnt a1 = [ 10, 20, 30, 40 ];const a2 = [ ...a1, 50]; // Extracti
4 min read
What Is YAML In Kubernetes ? Kubernetes is dependent on YAML (YAML Ain't Markup Language) as its configuration language. YAML makes managing and deploying Kubernetes more easy and simple. In this article, we'll cover all the aspects of using YAML in Kubernetes. Understanding YAMLYAML is a human-readable format of data serializa
7 min read
What Type of Language is YAML? As we know the world of technology is filled with various modern and old languages which keep changing from time to time, each one of these languages serves a specific purpose and thus each language is used for performing certain tasks only. But when it comes to the programming language called YAML
10 min read
What does <%= mean in EJS Template Engine ? EJS or Embedded Javascript Templating is a templating engine used by Node.js. Template engine helps to create an HTML template with minimal code. Also, it can inject data into an HTML template on the client side and produce the final HTML. EJS is a simple templating language that is used to generate
3 min read