Add a New Twig Function in Patternlab


Sometimes, you've got a Drupal twig function that doesn't exist in PatternLab, and it prevents PatternLab from building.

You get this error:

PHP Fatal error:  Uncaught Twig_Error_Syntax: Unknown "typogrify" filter in {code here}

What you need to do is create a Twig stub that does either what you need it to, or nothing at all. In emulsify, my twig functions exist at emulsify/components/_twig-components/filters.

I am creating a typogrify filter, and so I created a file called typogrify.filter.php. It looks like this:

 * @file
 * Add "typogrify" filter for Pattern Lab.
$filter = new Twig_SimpleFilter('typogrify', function ($string) {
  return $string;

You can do anything you like here with PHP. However, I only want PatternLab to compile, I don't actually need to do any fancy typogrify stuff. Therefore, this filter merely returns the string that was passed to it as an argument.

Mischief managed!

About the Author

Hi. My name is Jeremiah John. I'm a sf/f writer and activist.

I just completed a dystopian science fiction novel. I run a website which I created that connects farms with churches, mosques, and synagogues to buy fresh vegetables directly and distribute them on a sliding scale to those in need.

In 2003, I spent six months in prison for civil disobedience while working to close the School of the Americas, converting to Christianity, as one does, while I was in the clink.