From 9f05ca159179a4bd65a45bdae81948197bfdb7cb Mon Sep 17 00:00:00 2001 From: Michael Angstadt Date: Mon, 4 Aug 2014 19:31:39 -0500 Subject: [PATCH] Added handlebars helper to accept a context var which is itself a partial and hydrate it with itself. --- lib/upgrayedd.js | 15 ++++++++ test/fixture/render/upgrayedd/gold.html | 5 +++ test/fixture/render/upgrayedd/upgrayedd.html | 5 +++ test/tests/upgrayedd.js | 39 ++++++++++++++++++++ 4 files changed, 64 insertions(+) create mode 100644 test/fixture/render/upgrayedd/gold.html create mode 100644 test/fixture/render/upgrayedd/upgrayedd.html create mode 100644 test/tests/upgrayedd.js diff --git a/lib/upgrayedd.js b/lib/upgrayedd.js index 475af28..3c0b6ed 100644 --- a/lib/upgrayedd.js +++ b/lib/upgrayedd.js @@ -100,4 +100,19 @@ module.exports = function(Handlebars) { return ps[path](ctx, hash); }); + //Allowing for a handlebars variable to hold a partial that then is filled with the owning context's values + //ie, { context:"What a wonderful {{title}} at ${{price}}", title: "Smock", price: "5.34" } + //called like {{hydrate "context" this}} renders "What a wonderful Smock at $5.34" + Handlebars.registerHelper('hydrate', function(partial_var, ctx) { + Handlebars.registerPartial(partial_var, ctx[partial_var]); + + var ps = Handlebars.partials; + + if (typeof ps[partial_var] != 'function') + ps[partial_var] = Handlebars.compile(ps[partial_var]); + + return ps[partial_var](ctx); + + }); + }; \ No newline at end of file diff --git a/test/fixture/render/upgrayedd/gold.html b/test/fixture/render/upgrayedd/gold.html new file mode 100644 index 0000000..3680143 --- /dev/null +++ b/test/fixture/render/upgrayedd/gold.html @@ -0,0 +1,5 @@ +What a great Smock, + +And at only $5.34! + +I just can not believe it. \ No newline at end of file diff --git a/test/fixture/render/upgrayedd/upgrayedd.html b/test/fixture/render/upgrayedd/upgrayedd.html new file mode 100644 index 0000000..0ddfd36 --- /dev/null +++ b/test/fixture/render/upgrayedd/upgrayedd.html @@ -0,0 +1,5 @@ +What a great {{title}}, + +{{hydrate "content" this}} + +I just can not believe it. \ No newline at end of file diff --git a/test/tests/upgrayedd.js b/test/tests/upgrayedd.js new file mode 100644 index 0000000..2d1f803 --- /dev/null +++ b/test/tests/upgrayedd.js @@ -0,0 +1,39 @@ +var App = require('mixdown-app').App; +var YourPlugin = require('../../index.js'); +var assert = require('assert'); +var fs = require('fs'); +var gold = fs.readFileSync(require.resolve('../fixture/render/upgrayedd/gold.html'), 'utf-8'); + +suite('Sweet: Upgrayedd', function() { + var app = new App(); + + setup(function(done) { + this.timeout(10000); + + // create plugin + var p = new YourPlugin({ + + views: { + base: ['./test/fixture/render/upgrayedd'], + ext: "html" + } + }); + + // attach it + app.use(p); + + app.setup(done); + }); + + test('Hydrate helper', function(done) { + debugger; + var html = app.view_engine.render('upgrayedd', { + title: "Smock", + price: "5.34", + content: "And at only ${{price}}!" + }); + + assert.equal(html, gold, 'Content should match gold'); + done(); + }); +}); \ No newline at end of file