Welcome to the third episode in our video series for Emulsify 2.x. Emulsify 2.x is a new release that embodies our commitment to component-driven design within Drupal. We’ve added Composer and Drush support, as well as open-source Twig functions and many other changes to increase ease-of-use.
In this video, we’re going to teach you how Emulsify works with the BEM Twig extension. This blog post accompanies a tutorial video, embedded at the end of this post.
Background
In Emulsify 2.x, we have enhanced our support for BEM in Drupal by creating the BEM Twig extension. The BEM Twig extension makes it easy to deliver classes to both Pattern Lab and Drupal while using Drupal’s Attributes object. It also has the benefit of simplifying our syntax greatly. See the code below.
Emulsify 1.x:
{% set paragraph_base_class_var = paragraph_base_class|default('paragraph') %} {% set paragraph_modifiers = ['large', 'red'] %} <p class="{{ paragraph_base_class_var }}{% for modifier in paragraph_modifiers %} {{ paragraph_base_class_var }}--{{ modifier }}{% endfor %}{% if paragraph_blockname %} {{ paragraph_blockname }}__{{ paragraph_base_class_var }}{% endif %}"> {% block paragraph_content %} {{ paragraph_content }} {% endblock %} </p>
Emulsify 2.x:
<p {{ bem('paragraph', ['large', 'red']) }}> {% block paragraph_content %} {{ paragraph_content }} {% endblock %} </p>
In both Pattern Lab and Drupal, this function above will create p class=”paragraph paragraph--large paragraph--red”
, but in Drupal it will use the equivalent of p{{ attributes.addClass('paragraph paragraph--large paragraph--red') }}
, appending these classes to whatever classes core or other plugins provide as well. Simpler syntax + Drupal Attributes support!
We have released the BEM Twig function open source under the Drupal Pattern Lab initiative. It is in Emulsify 2.x by default, but we wanted other projects to be able to benefit from it as well.
Usage
The BEM Twig function accepts four arguments, only one of which is required.
Simple block name:h1 {{ bem('title') }}
In Drupal and Pattern Lab, this will print:
h1 class="title"
Block with modifiers (optional array allowing multiple modifiers):
h1 {{ bem('title', ['small', 'red']) }}
This creates:
h1 class="title title--small title--red"
Element with modifiers and block name (optional):
h1 {{ bem('title', ['small', 'red'], 'card') }}
This creates:
h1 class="card__title card__title--small card__title--red"
Element with block name, but no modifiers (optional):
h1 {{ bem('title', '', 'card') }}
This creates:
h1 class="card__title"
Element with modifiers, block name and extra classes (optional, in case you need non-BEM classes):
h1 {{ bem('title', ['small', 'red'], 'card', ['js-click', 'something-else']) }}
This creates:
h1 class="card__title card__title--small card__title--red js-click something-else"
Element with extra classes only (optional):
h1 {{ bem('title', '', '', ['js-click']) }}
This creates:
h1 class="title js-click"
Ba da BEM, ba da boom
With the new BEM Twig extension that we’ve added to Emulsify 2.x, you can easily deliver classes to Pattern Lab and Drupal, while keeping a nice, simple syntax. Thanks for following along! Make sure you check out the other posts in this series and their video tutorials as well!
Thanks for following our Emulsify 2.x tutorials. Miss a post? Read the full series:
- Part 1: Installing Emulsify
- Part 2: Creating your Emulsify 2.0 Starter Kit with Drush
- Part 3: BEM Twig function
- Part 4: DRY Twig approach
- Part 5: Building a full site header in Drupal
Watch all the videos on our YouTube channel.
Making the web a better place to teach, learn, and advocate starts here...
When you subscribe to our newsletter!