|
| 1 | +/* |
| 2 | + * underscore pattern engine for patternlab-node - v0.15.1 - 2015 |
| 3 | + * |
| 4 | + * Geoffrey Pursell, Brian Muenzenmeyer, and the web community. |
| 5 | + * Licensed under the MIT license. |
| 6 | + * |
| 7 | + * Many thanks to Brad Frost and Dave Olsen for inspiration, encouragement, and advice. |
| 8 | + * |
| 9 | + */ |
| 10 | + |
| 11 | + |
| 12 | +/* |
| 13 | + * ENGINE SUPPORT LEVEL: |
| 14 | + * |
| 15 | + * Basic. We can't call partials from inside underscore templates yet, but we |
| 16 | + * can render templates with backing JSON. |
| 17 | + * |
| 18 | + */ |
| 19 | + |
| 20 | +(function () { |
| 21 | + "use strict"; |
| 22 | + |
| 23 | + var _ = require('underscore'); |
| 24 | + |
| 25 | + // extend underscore with partial-ing methods |
| 26 | + // HANDLESCORE! UNDERBARS! |
| 27 | + _.mixin({ |
| 28 | + renderPartial: function(partial, data) { |
| 29 | + var data = data || {}; |
| 30 | + var compiled = _.template(partial); |
| 31 | + return compiled(data); |
| 32 | + }, |
| 33 | + assignContext: function(viewModel, data) { |
| 34 | + return viewModel(data); |
| 35 | + } |
| 36 | + }); |
| 37 | + |
| 38 | + var engine_underscore = { |
| 39 | + engine: _, |
| 40 | + engineName: 'underscore', |
| 41 | + engineFileExtension: '.underscore', |
| 42 | + |
| 43 | + // partial expansion is only necessary for Mustache templates that have |
| 44 | + // style modifiers or pattern parameters (I think) |
| 45 | + expandPartials: false, |
| 46 | + |
| 47 | + // regexes, stored here so they're only compiled once |
| 48 | + findPartialsRE: /<%= _.renderPartial\((.*?)\).*?%>/g, // TODO, |
| 49 | + findPartialsWithStyleModifiersRE: /<%= _.renderPartial\((.*?)\).*?%>/g, // TODO |
| 50 | + findPartialsWithPatternParametersRE: /<%= _.renderPartial\((.*?)\).*?%>/g, // TODO |
| 51 | + findListItemsRE: /({{#( )?)(list(I|i)tems.)(one|two|three|four|five|six|seven|eight|nine|ten|eleven|twelve|thirteen|fourteen|fifteen|sixteen|seventeen|eighteen|nineteen|twenty)( )?}}/g, |
| 52 | + |
| 53 | + // render it |
| 54 | + renderPattern: function renderPattern(template, data, partials) { |
| 55 | + var compiled = _.template(template); |
| 56 | + return compiled(_.extend(data, { |
| 57 | + _allData: data, |
| 58 | + _partials: partials |
| 59 | + })); |
| 60 | + }, |
| 61 | + |
| 62 | + // registerPartial: function (oPattern) { |
| 63 | + // debugger; |
| 64 | + // _.registerPartial(oPattern.key, oPattern.template); |
| 65 | + // }, |
| 66 | + |
| 67 | + // find and return any {{> template-name }} within pattern |
| 68 | + findPartials: function findPartials(pattern) { |
| 69 | + var matches = pattern.template.match(this.findPartialsRE); |
| 70 | + return matches; |
| 71 | + }, |
| 72 | + findPartialsWithStyleModifiers: function(pattern) { |
| 73 | + return []; |
| 74 | + }, |
| 75 | + // returns any patterns that match {{> value(foo:"bar") }} or {{> |
| 76 | + // value:mod(foo:"bar") }} within the pattern |
| 77 | + findPartialsWithPatternParameters: function(pattern) { |
| 78 | + return []; |
| 79 | + }, |
| 80 | + findListItems: function(pattern) { |
| 81 | + var matches = pattern.template.match(this.findListItemsRE); |
| 82 | + return matches; |
| 83 | + }, |
| 84 | + // given a pattern, and a partial string, tease out the "pattern key" and |
| 85 | + // return it. |
| 86 | + findPartialKey: function(partialString) { |
| 87 | + var partialKey = partialString.replace(this.findPartialsRE, '$1'); |
| 88 | + return partialKey; |
| 89 | + } |
| 90 | + }; |
| 91 | + |
| 92 | + module.exports = engine_underscore; |
| 93 | +})(); |
0 commit comments