Skip to content

Drupal 8, Meet YAML and Twig

By GRAYBOX Alumni

Building any kind of website is not a simple endeavor, and the latest major upgrade of Drupal to version 8 has only further solidified that truth. Even if you knew Drupal 7 you're in trouble because the biggest difference from version 7 to 8 is...well...pretty much everything.

In this article we'll take a focused look at Drupal 8's new technologies. We're not going to cover any of the specifics of installing and setting up Drupal 8, or the logic behind theme and sub-theme creation‚ plenty of other sites cover these topics and offer tutorials. Instead we're going to look at YAML and Twig.

Twig Drupal 8

creative commons license by Avia Venefica

YAML

Where does YAML fit in? After Drupal is installed and the initial setup is complete, you start up your theme environment which ties everything together. In previous versions of Drupal, there were the all-important .info files which allowed you to configure all the necessary elements to make your Drupal theme function. Now instead of .info, it's all YAML. Welcome to the new world.

What is YAML?

"YAML™ (rhymes with "camel") is a human-friendly, cross language, Unicode based data serialization language designed around the common native data structures of agile programming languages. It is broadly useful for programming needs ranging from configuration files to Internet messaging to object persistence to data auditing. Together with the Unicode standard for characters, the YAML specification provides all the information necessary to understand YAML Version 1.2 and to creating programs that process YAML information." [source: yaml.org/spec/]

So, if you're familiar with JSON, YAML is JSON on steroids and offers more features and, some would argue, is easier on the eyes.

An example of a YAML file contents would look something like this...

name: ACME

type: theme

base theme: business

description: 'A customized ACME theme.'

package: Core

version: VERSION

core: 8.x

libraries:

- acme/global-styling

- acme/corescript

regions:

headline: 'Headline'

breadcrumb: 'Breadcrumb'

This YAML file will reside in your custom theme folder and will govern how everything is referenced and mapped including items like your libraries (css, js, etc.) and the different template blocks that you create.

For more on YAML, check out http://www.yaml.org/start.html

Twig

The next big change from Drupal 7 to 8 is the move from PHPTemplate to Symfony2 (specifically Twig as the implementation) as the framework and language you use when building/customizing your Drupal 8 theme.

What is Symfony?

Symfony is a set of PHP Components and a Web Application framework.

For more information on Symfony, check out https://symfony.com/

What is (Drupal) Twig?

"Twig is a PHP-based compiled templating language…— "The Drupal Twig initiative shares the same motivation as the Symfony initiative: to adopt a modern, powerful, OOP-based engine that will allow developers to concentrate on Drupal proper." [source https://www.drupal.org/node/1918824]

Some of the bigger distinguishing characteristics of Twig when you look at the code is the use of the double curly brackets...

{{ page.content }}

…and the curly bracket with a percentage sign...

{% if something.here %}

Something shows


{% endif %}

Here's an example of Twig from a template page on a Drupal 8 site theme:

Drupal 8 site theme

The change that Twig brings to Drupal coding is substantially different from Drupal versions 7 and earlier because that it changes how you create your templates. The learning curve of Drupal 8 is medium to high depending on your familiarity with Symfony and/or Twig. With each new version, the folks at Drupal have made decisions to use certain technologies to improve the CMS platform. With this update, they have decided to essentially strip down the core Drupal platform and then rebuild it using a new paradigm.

Impact

Whether this big shift works in the long run or not remains to be seen. The biggest barrier to-date standing in the way of large-scale Drupal 8 adoption is that developers are still working on getting their modules and extensions Drupal 8-ready. This gap will be filled as more and more users adopt Drupal as a CMS or upgrade their Drupal sites and the demand for Drupal 8-ready plugins grows.

All the signs indicate that Drupal 8 is already a big hit and will continue to gain both new adopters as well as those currently on previous versions of the CMS platform.

Blog & Events

Featured Work