Is it bad practice to print fields that will use Drupal WYSIWYG in Pattern Lab using |raw?

August 28, 2019


This is a question I recently asked in the Drupaltwig Slack #pattern-lab channel.

Some background: I recently had a situation where I had a "Page introduction" component. This component has 1 field, and it's a WYSIWYG field, which means it needs to print HTML.

Here is the structure of my component:

|- page-introduction.twig
|- page-introduction.scss
|- page-introduction.yml

Within my page-introduction.yml file, I put some HTML within the field that represents the WYSIWYG, only to have Pattern Lab not render the HTML, but print it out literally.

The way I "fixed" this was by using the |raw filter.

Something about it felt ... wrong though. So I decided to ask for some help in the #pattern-lab channel of the Drupaltwig Slack.

After some discussion, Mark Conroy helped me figure out that I'm double escaping my HTML. I had a setting in my pattern-lab/config/config.yml with twigAutoescape: true

This escaped my HTML by default with meant I had to use |raw in order to get it to render. By setting twigAutoescape to false, I no longer needed to use |raw.

While there may be some times you need to use |raw, this was not one of those times.