From dda9fdd5a9b18bf27c92054c501d3a8774d53ea1 Mon Sep 17 00:00:00 2001 From: Jayson Potter Date: Thu, 15 Mar 2018 22:15:45 -0700 Subject: [PATCH 1/2] Partials Underscore Compile Made a small modification to the underscore support render method to allow the template partials to get compiled allowing data to get passed to those as well. var user = {username: 'Beetlejuice'}; <%= partials.xanadu(user) %> --- lib/consolidate.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/consolidate.js b/lib/consolidate.js index 37386bf..70612ff 100644 --- a/lib/consolidate.js +++ b/lib/consolidate.js @@ -901,6 +901,9 @@ exports.underscore.render = function(str, options, fn) { return promisify(fn, function(fn) { var engine = requires.underscore || (requires.underscore = require('underscore')); try { + for (var partial in options.partials) { + options.partials[partial] = engine.template(options.partials[partial]); + } var tmpl = cache(options) || cache(options, engine.template(str, null, options)); fn(null, tmpl(options).replace(/\n$/, '')); } catch (err) { From f9a17902730cae6c0655e01da1f5689f3bd32fe7 Mon Sep 17 00:00:00 2001 From: Jayson Potter Date: Fri, 16 Mar 2018 11:12:42 -0700 Subject: [PATCH 2/2] Added test fixtures for underscore partial support Added test fixtures and a test for the added support of underscore partials. --- test/consolidate.js | 1 + test/fixtures/underscore/partials.underscore | 1 + .../underscore/user_partial.underscore | 1 + test/shared/partials.js | 18 ++++++++++++++++-- 4 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 test/fixtures/underscore/partials.underscore create mode 100644 test/fixtures/underscore/user_partial.underscore diff --git a/test/consolidate.js b/test/consolidate.js index a900f8e..375bacc 100644 --- a/test/consolidate.js +++ b/test/consolidate.js @@ -35,6 +35,7 @@ require('./shared').test('handlebars'); require('./shared/partials').test('handlebars'); require('./shared/helpers').test('handlebars'); require('./shared').test('underscore'); +require('./shared/partials').test('underscore'); require('./shared').test('lodash'); require('./shared').test('qejs'); require('./shared').test('walrus'); diff --git a/test/fixtures/underscore/partials.underscore b/test/fixtures/underscore/partials.underscore new file mode 100644 index 0000000..472c5ae --- /dev/null +++ b/test/fixtures/underscore/partials.underscore @@ -0,0 +1 @@ +

<%- user.name %>

<%= partials.partial(user) %> \ No newline at end of file diff --git a/test/fixtures/underscore/user_partial.underscore b/test/fixtures/underscore/user_partial.underscore new file mode 100644 index 0000000..5e42261 --- /dev/null +++ b/test/fixtures/underscore/user_partial.underscore @@ -0,0 +1 @@ +

<%- name %> from user_partial!

\ No newline at end of file diff --git a/test/shared/partials.js b/test/shared/partials.js index 5982b6c..1ff4e52 100644 --- a/test/shared/partials.js +++ b/test/shared/partials.js @@ -41,8 +41,22 @@ exports.test = function(name) { done(); }); }); - } - else { + } else if (name == 'underscore') { + it('should support partials', function(done){ + var path = 'test/fixtures/' + name + '/partials.' + name; + var locals = { + user: user, + partials: { + partial: 'user_partial' + } + }; + cons[name](path, locals, function(err, html){ + if (err) return done(err); + html.should.equal('

Tobi

Tobi from user_partial!

'); + done(); + }); + }); + } else { it('should support rendering a partial', function(done){ var str = fs.readFileSync('test/fixtures/' + name + '/user_partial.' + name).toString(); var locals = {