|
1 |
| -## The Underscore engine for Pattern Lab / Node |
| 1 | +# The Underscore engine for Pattern Lab / Node |
2 | 2 |
|
3 | 3 | To install the Underscore engine in your edition, `npm install patternengine-node-underscore` should do the trick.
|
4 | 4 |
|
5 |
| -Level of support is basic; you can author patterns in Underscore, but support for calling pattern partials is accomplished through an Underscore mixin, and is considered experimental. |
| 5 | +## Supported features |
| 6 | +- [x] [Includes](http://patternlab.io/docs/pattern-including.html) (Accomplished using the included [`_.renderNamedPartial()`](https://github.com/pattern-lab/patternengine-node-underscore/blob/master/lib/engine_underscore.js#L54-L59)) |
| 7 | +- [x] Lineage |
| 8 | +- [x] [Hidden Patterns](http://patternlab.io/docs/pattern-hiding.html) |
| 9 | +- [x] [Pseudo-Patterns](http://patternlab.io/docs/pattern-pseudo-patterns.html) |
| 10 | +- [x] [Pattern States](http://patternlab.io/docs/pattern-states.html) |
| 11 | +- [ ] [Pattern Parameters](http://patternlab.io/docs/pattern-parameters.html) (Accomplished instead using parameter object passed to the included [`_.renderNamedPartial()`](https://github.com/pattern-lab/patternengine-node-underscore/blob/master/lib/engine_underscore.js#L54-L59) mixin function) |
| 12 | +- [ ] [Style Modifiers](http://patternlab.io/docs/pattern-stylemodifier.html) (Accomplished instead using parameter object passed to the included [`_.renderNamedPartial()`](https://github.com/pattern-lab/patternengine-node-underscore/blob/master/lib/engine_underscore.js#L54-L59) mixin function) |
| 13 | + |
| 14 | +## The extensions to basic Underscore functionality. |
| 15 | + |
| 16 | +### Pattern including |
| 17 | +Underscore templates include no native support for calling other templates, so support for pattern including is accomplished through an included Underscore mixin function, [`_.renderNamedPartial()`](https://github.com/pattern-lab/patternengine-node-underscore/blob/master/lib/engine_underscore.js#L54-L59), and is considered experimental, but seems to work just fine. |
| 18 | + |
| 19 | +#### Example |
| 20 | +``` |
| 21 | +<p> |
| 22 | + Here's a large button, with parameters: |
| 23 | + <%- _.renderNamedPartial('atoms-button', { variantClass: 'btn-large' }) %> |
| 24 | +</p> |
| 25 | +``` |
| 26 | + |
| 27 | +### Safely referring to deeply nested data in the pattern JSON |
| 28 | +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: |
| 29 | +```json |
| 30 | +{ |
| 31 | + "foo": { |
| 32 | + "bar": { |
| 33 | + "value": "That is the question:" |
| 34 | + } |
| 35 | + } |
| 36 | +} |
| 37 | +``` |
| 38 | +And the following in an underscore template that refers to it: |
| 39 | +``` |
| 40 | +<p> |
| 41 | + To be, or not to be, <%= foo.bar.value %> |
| 42 | +</p> |
| 43 | +``` |
| 44 | + |
| 45 | +If you feed that template JSON that (for whatever reason) has `foo.bar` as `null`, the pattern will crash because `null.value` throws an exception. It's nice to be able to write this instead: |
| 46 | +``` |
| 47 | +<p> |
| 48 | + To be, or not to be, <%= _.getPath('foo.bar.value', obj) %> |
| 49 | +</p> |
| 50 | +``` |
| 51 | + |
| 52 | +and know that the output will be more safely "To be, or, not to be, null" instead of just throwing and error and crashing the pattern. This is mainly useful for operationalized templates that will be provided with JSON from services that you can't control in Pattern Lab. |
| 53 | + |
| 54 | +Note that `obj` is an Underscore pattern's current data context. See [Dr. Axel Rauschmeyer's article](http://www.2ality.com/2012/06/underscore-templates.html) for more. |
0 commit comments