An Introduction to YAML

Video loading...

Join Drupalize.Me to watch this video

Join today and gain instant access to our entire video library.

Log in Sign up
  • 0:06
    As soon as you start writing code for Drupal 8,
  • 0:09
    you're going to encounter files that end in a .yml extension.
  • 0:13
    These are YAML files.
  • 0:15
    YAML is a data serialization format.
  • 0:17
    And I want to talk about why YAML and then look at the YAML syntax
  • 0:21
    so that when you encounter these files in all the various places
  • 0:24
    they're used Drupal, you'll have a better
  • 0:26
    understanding of what you're looking at.
  • 0:28
    In this tutorial, we'll look at why YAML is good for data
  • 0:31
    serialization, the YAML syntax, and then do some experimenting

An Introduction to YAML

Loading...

YAML, which stands for YAML Ain't Markup Language, is a human-readable data serialization format that's been widely adopted in a variety of use cases in Drupal 8. Anyone wanting to write modules, or themes, for Drupal 8 will need to understand YAML syntax. Even site-builders are likely to encounter YAML at least in passing as YAML is the data-serialization format of choice for Drupal 8's configuration management system. Good thing it's pretty easy to learn even with the most basic of programming backgrounds.

This tutorial will look at the YAML data format and provide examples of how to write and read YAML. Starting with an introduction to the language's syntax and some of the strengths of YAML. Then looking at the difference between scalar data types like strings and integers, and collection data types like lists and associative arrays.

Since YAML in the Drupal world is read into PHP and ultimately becomes a PHP data structure that we can use in our own code we'll also look at how the YAML we write in a .yml file is represented in PHP data types. To do this we'll use the YAML Sandbox module that provides a handy textarea into which we can type YAML and have it parsed into PHP data structures.

Learning Objectives

  • Explain what YAML is and it's strengths as a data serialization format
  • Create scalar key/value pairs in YAML
  • Create lists, and associative arrays using YAML collections
  • Understand how the YAML you write is represented in PHP