Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions lib/upgrayedd.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

});

};
5 changes: 5 additions & 0 deletions test/fixture/render/upgrayedd/gold.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
What a great Smock,

And at only $5.34!

I just can not believe it.
5 changes: 5 additions & 0 deletions test/fixture/render/upgrayedd/upgrayedd.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
What a great {{title}},

{{hydrate "content" this}}

I just can not believe it.
39 changes: 39 additions & 0 deletions test/tests/upgrayedd.js
Original file line number Diff line number Diff line change
@@ -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();
});
});