The Underscore engine for Patternlab/Node
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
lib Shift template compilation to registerPartial() Oct 12, 2016
.editorconfig add .editorconfig and .eslintrc from pattern-engines branch Feb 27, 2016
package.json fix version number in package.json Oct 12, 2016

The Underscore engine for Pattern Lab / Node


To install the Underscore engine in your edition, npm install patternengine-node-underscore should do the trick.

Supported features

Extensions to basic Underscore functionality

Pattern including

Underscore templates include no native support for calling other templates, so support for pattern including is accomplished through an included Underscore mixin function, _.renderNamedPartial(), and is considered experimental, but seems to work just fine.


  Here's a large button, with parameters: 
  <%- _.renderNamedPartial('atoms-button', { variantClass: 'btn-large' }) %>

Safely referring to deeply nested data in the pattern JSON

When referring to deeply nested data, it's helpful to have a way of doing that (as Handlebars and Mustache do) that's tolerant of unexpected null values. For example, if you have the following pattern JSON:

  "foo": {
    "bar": {
      "value": "That is the question:"

And the following in an underscore template that refers to it:

  To be, or not to be, <%= %>

If you feed that template JSON that (for whatever reason) has as null, the pattern will crash because null.value throws an exception. It's nice to be able to write this instead:

  To be, or not to be, <%= _.getPath('', obj) %>

and know that the output will be more safely "To be, or, not to be, null" instead of just throwing an error and crashing the pattern. This is mainly useful for operationalized pattern templates that will be provided with JSON from services that you can't control in Pattern Lab.

Note that obj is an Underscore pattern's current data context. See Dr. Axel Rauschmeyer's article for more.