Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Shell Scripting
  • Kali Linux
  • Ubuntu
  • Red Hat
  • CentOS
  • Docker in Linux
  • Kubernetes in Linux
  • Linux interview question
  • Python
  • R
  • Java
  • C
  • C++
  • JavaScript
  • DSA
Open In App
Next Article:
What does {{ }} mean in YAML?
Next article icon

What does {{ }} mean in YAML?

Last Updated : 10 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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:

  1. Retrieve the value of the "SOME_ENV_VARIABLE" environment variable.
  2. Convert it to uppercase.
  3. 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.


Next Article
What does {{ }} mean in YAML?

S

srivastavas260
Improve
Article Tags :
  • Linux-Unix
  • Geeks Premier League
  • Geeks Premier League 2023

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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences