From 88c24c9b0adb0c83f22cbd0d6acbb8a4741135db Mon Sep 17 00:00:00 2001 From: Nik Voss Date: Thu, 13 Feb 2014 22:35:26 +0000 Subject: [PATCH 1/5] support for handlebars templates --- lib/helpers/raw.js | 2 +- lib/template/processors/hbs.js | 28 ++++++++++++++++++++++++++++ package.json | 3 ++- 3 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 lib/template/processors/hbs.js diff --git a/lib/helpers/raw.js b/lib/helpers/raw.js index 5a0bd006b..b04454be6 100644 --- a/lib/helpers/raw.js +++ b/lib/helpers/raw.js @@ -13,7 +13,7 @@ var TerraformError = exports.TerraformError = require("../error").TerraformError */ var processors = exports.processors = { - "html": ["jade", "ejs", "md"], + "html": ["jade", "ejs", "md", "hbs"], "css" : ["styl", "less", "scss", "sass"], "js" : ["coffee"] } diff --git a/lib/template/processors/hbs.js b/lib/template/processors/hbs.js new file mode 100644 index 000000000..988f0d9d9 --- /dev/null +++ b/lib/template/processors/hbs.js @@ -0,0 +1,28 @@ +var Handlebars = require('handlebars') +var TerraformError = require("../../error").TerraformError + +module.exports = function(fileContents, options){ + + return { + compile: function(){ + return Handlebars.compile(fileContents.toString(), options) + }, + + parseError: function(error){ + + var arr = error.message.split("\n") + var path_arr = arr[0].split(":") + + error.lineno = parseInt(error.lineno || path_arr[path_arr.length -1] || -1) + error.message = arr[arr.length - 1] + error.name = error.name + error.source = "Handlebars" + error.dest = "HTML" + error.filename = error.path || options.filename + error.stack = fileContents.toString() + + return new TerraformError(error) + } + } + +} diff --git a/package.json b/package.json index d74702f8f..469472948 100644 --- a/package.json +++ b/package.json @@ -37,7 +37,8 @@ "less": "2.5.0", "stylus": "0.47.3", "minify": "git://github.com/kennethormandy/minify#v0.3.0", - "autoprefixer": "5.1.0" + "autoprefixer": "5.1.0", + "handlebars": "1.3.0" }, "devDependencies": { "mocha": "1.8.2", From d37a4fb9af85e010ecc93e4f4d5d3d1acf987be4 Mon Sep 17 00:00:00 2001 From: Marios Antonoudiou Date: Wed, 24 Sep 2014 13:10:12 +0000 Subject: [PATCH 2/5] Add handlebars (.hbs) support --- lib/template/processors/hbs.js | 2 +- package.json | 2 +- test/fixtures/templates/content.hbs | 1 + test/helpers.js | 17 +++++++++-------- test/templates.js | 10 ++++++++++ 5 files changed, 22 insertions(+), 10 deletions(-) create mode 100644 test/fixtures/templates/content.hbs diff --git a/lib/template/processors/hbs.js b/lib/template/processors/hbs.js index 988f0d9d9..8222ca82c 100644 --- a/lib/template/processors/hbs.js +++ b/lib/template/processors/hbs.js @@ -16,7 +16,7 @@ module.exports = function(fileContents, options){ error.lineno = parseInt(error.lineno || path_arr[path_arr.length -1] || -1) error.message = arr[arr.length - 1] error.name = error.name - error.source = "Handlebars" + error.source = "HBS" error.dest = "HTML" error.filename = error.path || options.filename error.stack = fileContents.toString() diff --git a/package.json b/package.json index 469472948..86c6fa968 100644 --- a/package.json +++ b/package.json @@ -38,7 +38,7 @@ "stylus": "0.47.3", "minify": "git://github.com/kennethormandy/minify#v0.3.0", "autoprefixer": "5.1.0", - "handlebars": "1.3.0" + "handlebars": "^2.0.0" }, "devDependencies": { "mocha": "1.8.2", diff --git a/test/fixtures/templates/content.hbs b/test/fixtures/templates/content.hbs new file mode 100644 index 000000000..1a614b8d5 --- /dev/null +++ b/test/fixtures/templates/content.hbs @@ -0,0 +1 @@ +

Hello! This is {{!-- should be invisible --}}Handlebars!

diff --git a/test/helpers.js b/test/helpers.js index f7dbf3075..da2603190 100644 --- a/test/helpers.js +++ b/test/helpers.js @@ -9,8 +9,8 @@ describe("helpers", function(){ it('should return all possible file names for html ordered by priority.', function(done){ var list = polymer.helpers.buildPriorityList('index.html') list.should.be.an.instanceOf(Array) - list.should.have.lengthOf(6) - var plist = "index.jade, index.ejs, index.md, index.html.jade, index.html.ejs, index.html.md".split(', ') + list.should.have.lengthOf(8) + var plist = "index.jade, index.ejs, index.md, index.hbs, index.html.jade, index.html.ejs, index.html.md, index.html.hbs".split(', ') list.should.eql(plist) done() }) @@ -26,27 +26,28 @@ describe("helpers", function(){ it('should build priority list assuming template file when unknown.', function(done){ var list = polymer.helpers.buildPriorityList('feed.xml') list.should.be.an.instanceOf(Array) - list.should.have.lengthOf(3) - list.should.eql('feed.xml.jade, feed.xml.ejs, feed.xml.md'. split(', ')) + list.should.have.lengthOf(4) + list.should.eql('feed.xml.jade, feed.xml.ejs, feed.xml.md, feed.xml.hbs'. split(', ')) done() }) it('should look for templates on json files.', function(done){ var list = polymer.helpers.buildPriorityList('profile.json') list.should.be.an.instanceOf(Array) - list.should.have.lengthOf(3) + list.should.have.lengthOf(4) list.should.include('profile.json.jade') list.should.include('profile.json.ejs') list.should.include('profile.json.md') - list.should.eql('profile.json.jade, profile.json.ejs, profile.json.md'. split(', ')) + list.should.include('profile.json.hbs') + list.should.eql('profile.json.jade, profile.json.ejs, profile.json.md, profile.json.hbs'. split(', ')) done() }) it('should look for templates when no ext present.', function(done){ var list = polymer.helpers.buildPriorityList('appcache') list.should.be.an.instanceOf(Array) - list.should.have.lengthOf(3) - list.should.eql('appcache.jade, appcache.ejs, appcache.md'.split(', ')) + list.should.have.lengthOf(4) + list.should.eql('appcache.jade, appcache.ejs, appcache.md, appcache.hbs'.split(', ')) done() }) diff --git a/test/templates.js b/test/templates.js index 9bb436a1b..0c441eee0 100644 --- a/test/templates.js +++ b/test/templates.js @@ -25,6 +25,16 @@ describe("templates", function(){ }) }) + describe(".hbs", function(){ + it("should render handlebars file", function(done){ + poly.render("content.hbs", function(error, body){ + should.exist(body) + body.should.include("

Hello! This is Handlebars!

") + done() + }) + }) + }) + describe(".md", function(){ it("should render markdown file", function(done){ poly.render("stuff.md", function(error, body){ From b776fea38c1d4e81ccb7c208ec86aefbb33c617d Mon Sep 17 00:00:00 2001 From: Kenneth Ormandy Date: Mon, 24 Mar 2014 10:17:03 -0700 Subject: [PATCH 3/5] Test partials with data, too --- test/fixtures/templates/bio.ejs | 1 + test/fixtures/templates/hbs/partials-with-data.hbs | 2 ++ test/fixtures/templates/hbs/partials.hbs | 3 +++ test/fixtures/templates/index.jade | 2 +- test/fixtures/templates/profile.jade | 2 +- test/templates.js | 12 +++++++++--- 6 files changed, 17 insertions(+), 5 deletions(-) create mode 100644 test/fixtures/templates/hbs/partials-with-data.hbs create mode 100644 test/fixtures/templates/hbs/partials.hbs diff --git a/test/fixtures/templates/bio.ejs b/test/fixtures/templates/bio.ejs index 6d20d47f6..dc81b0237 100644 --- a/test/fixtures/templates/bio.ejs +++ b/test/fixtures/templates/bio.ejs @@ -5,3 +5,4 @@

Hello EJS

<%- partial("stuff.md") %> +<%- partial("profile.jade", { "title": "Brock Whitten" }) %> diff --git a/test/fixtures/templates/hbs/partials-with-data.hbs b/test/fixtures/templates/hbs/partials-with-data.hbs new file mode 100644 index 000000000..ae7f6f5d8 --- /dev/null +++ b/test/fixtures/templates/hbs/partials-with-data.hbs @@ -0,0 +1,2 @@ +

Hello Handlebars

+{{# partial ../profile.jade data="{ "title": "Brock Whitten" }" }} diff --git a/test/fixtures/templates/hbs/partials.hbs b/test/fixtures/templates/hbs/partials.hbs new file mode 100644 index 000000000..448cc763e --- /dev/null +++ b/test/fixtures/templates/hbs/partials.hbs @@ -0,0 +1,3 @@ +

Hello Handlebars

+{{# partial ../profile.jade }} +{{# partial ../stuff.md }} diff --git a/test/fixtures/templates/index.jade b/test/fixtures/templates/index.jade index eee22fc56..19e13809e 100644 --- a/test/fixtures/templates/index.jade +++ b/test/fixtures/templates/index.jade @@ -3,7 +3,7 @@ h2 Hello World -!= partial("profile.jade") +!= partial("profile.jade", { title: "Brock Whitten" }) != partial("stuff.md") h4= place pre diff --git a/test/fixtures/templates/profile.jade b/test/fixtures/templates/profile.jade index 5d358cf2b..95b34cc6f 100644 --- a/test/fixtures/templates/profile.jade +++ b/test/fixtures/templates/profile.jade @@ -1 +1 @@ -h3 Brock Whitten \ No newline at end of file +h3= title diff --git a/test/templates.js b/test/templates.js index 0c441eee0..b86ea37e8 100644 --- a/test/templates.js +++ b/test/templates.js @@ -124,10 +124,16 @@ describe("templates", function(){ poly.render("extend.jade", function(error, body){ should.not.exist(error) should.exist(body) - body.should.include("

Sintaxi

") - body.should.include("

Hello World

") + body.should.include("

hello markdown

") + done() + }) + }) + + it("should pass data into partials", function(done){ + poly.render("hbs/partials-with-data.jade", function(error, body){ + should.not.exist(error) + should.exist(body) body.should.include("

Brock Whitten

") - body.should.include("

Vancouver

") done() }) }) From 964d49e2362265356bc351252b605e8a19722ffb Mon Sep 17 00:00:00 2001 From: Max Kramer Date: Thu, 30 Apr 2015 11:40:55 -0400 Subject: [PATCH 4/5] Bump Handlebars to 3.0.3 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 86c6fa968..d3f96b532 100644 --- a/package.json +++ b/package.json @@ -38,7 +38,7 @@ "stylus": "0.47.3", "minify": "git://github.com/kennethormandy/minify#v0.3.0", "autoprefixer": "5.1.0", - "handlebars": "^2.0.0" + "handlebars": "^3.0.3" }, "devDependencies": { "mocha": "1.8.2", From 1deee797b83617a3ca1522e82d1e9f1de249b275 Mon Sep 17 00:00:00 2001 From: Max Kramer Date: Thu, 30 Apr 2015 11:43:25 -0400 Subject: [PATCH 5/5] Add partial support for handlebars --- lib/template/processors/hbs.js | 17 ++++++++++ test/fixtures/templates/bio.ejs | 1 - .../templates/hbs/partials-with-data.hbs | 2 +- test/fixtures/templates/hbs/partials.hbs | 3 +- test/templates.js | 33 ++++++++++++++----- 5 files changed, 43 insertions(+), 13 deletions(-) diff --git a/lib/template/processors/hbs.js b/lib/template/processors/hbs.js index 8222ca82c..3b201027e 100644 --- a/lib/template/processors/hbs.js +++ b/lib/template/processors/hbs.js @@ -3,6 +3,23 @@ var TerraformError = require("../../error").TerraformError module.exports = function(fileContents, options){ + /** + * Provides support to load partials. + * + * @usage without locals + * {{partial '../foo.md'}} + * + * @example with provided locals + * {{partial '../foo.jade' locals='{ "a": "b" }'}} + * + * @returns {Handlebars.SafeString} HTML-safe rendered partial + */ + Handlebars.registerHelper('partial', function(filePath, options) { + var locals = options.hash.locals || {}; + if (typeof locals === 'string') locals = JSON.parse(locals) + return new Handlebars.SafeString(this.partial(filePath, locals)) + }); + return { compile: function(){ return Handlebars.compile(fileContents.toString(), options) diff --git a/test/fixtures/templates/bio.ejs b/test/fixtures/templates/bio.ejs index dc81b0237..6d20d47f6 100644 --- a/test/fixtures/templates/bio.ejs +++ b/test/fixtures/templates/bio.ejs @@ -5,4 +5,3 @@

Hello EJS

<%- partial("stuff.md") %> -<%- partial("profile.jade", { "title": "Brock Whitten" }) %> diff --git a/test/fixtures/templates/hbs/partials-with-data.hbs b/test/fixtures/templates/hbs/partials-with-data.hbs index ae7f6f5d8..f0ec17581 100644 --- a/test/fixtures/templates/hbs/partials-with-data.hbs +++ b/test/fixtures/templates/hbs/partials-with-data.hbs @@ -1,2 +1,2 @@

Hello Handlebars

-{{# partial ../profile.jade data="{ "title": "Brock Whitten" }" }} +{{partial '../profile.jade' locals='{ "title": "Brock Whitten" }' }} diff --git a/test/fixtures/templates/hbs/partials.hbs b/test/fixtures/templates/hbs/partials.hbs index 448cc763e..ef4040556 100644 --- a/test/fixtures/templates/hbs/partials.hbs +++ b/test/fixtures/templates/hbs/partials.hbs @@ -1,3 +1,2 @@

Hello Handlebars

-{{# partial ../profile.jade }} -{{# partial ../stuff.md }} +{{partial '../stuff.md' }} diff --git a/test/templates.js b/test/templates.js index b86ea37e8..0d2ab0032 100644 --- a/test/templates.js +++ b/test/templates.js @@ -33,6 +33,30 @@ describe("templates", function(){ done() }) }) + + it("should render partials", function(done){ + poly.render("hbs/partials.hbs", function(error, body){ + should.not.exist(error) + should.exist(body) + // from main file + body.should.include("

Hello Handlebars

") + // from markdown partial + body.should.include("

hello markdown

") + done() + }) + }) + + it("should pass data into partials", function(done){ + poly.render("hbs/partials-with-data.hbs", function(error, body){ + should.not.exist(error) + should.exist(body) + // from main file + body.should.include("

Hello Handlebars

") + // from jade partial + body.should.include("

Brock Whitten

") + done() + }) + }) }) describe(".md", function(){ @@ -129,15 +153,6 @@ describe("templates", function(){ }) }) - it("should pass data into partials", function(done){ - poly.render("hbs/partials-with-data.jade", function(error, body){ - should.not.exist(error) - should.exist(body) - body.should.include("

Brock Whitten

") - done() - }) - }) - }) })