Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removal of delimiters and support for partials #2

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 16 additions & 2 deletions README.md
Expand Up @@ -17,10 +17,16 @@ class RobotGenerator extends yeoman.generators.Base
super
```

Settings Handlebar delimiters (default to '{{' and '}}'):
Setting custom helpers:
```coffee
handlebarsEngine = require 'yeoman-handlebars-engine'
theEngine = handlebarsEngine('<%=', '%>')
theEngine = handlebarsEngine {fns: {toLowerCase: (str) -> str.toLowerCase()}}
```

Setting directory where Handlebar partials reside:
```coffee
handlebarsEngine = require 'yeoman-handlebars-engine'
theEngine = handlebarsEngine {partials: 'partials/'}
```

Using Underscore.string helpers in your generator templates:
Expand All @@ -42,3 +48,11 @@ My class: RobotGenerator
Big number: 123,456,789.12300

```


Changelog
---------

#### v1.0

Breaking change: Export a function that configures Handlebars and returns an engine, instead of exporting the engine directly.
55 changes: 30 additions & 25 deletions handlebars_engine.js
@@ -1,34 +1,39 @@
var Handlebars = require('handlebars'),
handlebarsDelimiters = require('handlebars-delimiters'),
_ = require('underscore.string');
fs = require('fs');

// register underscore.string helpers
Object.keys(_).forEach(function(helper) {
var _sHelper = _[helper];
if(Handlebars.Utils.isFunction(_sHelper))
Handlebars.registerHelper(helper, function() {
var args = Array.prototype.slice.call(arguments, 0).slice(0, -1);
return _sHelper.apply(this, args);
});
});

var engine = module.exports = function (beginDelimiter, endDelimiter) {
var me = this;
module.exports = function(options) {
this.engine = function (source, data) {
return Handlebars.compile(source)(data);
};

this.setDelimiter = function(beginDelimiter, endDelimiter) {
me._beginDelimiter = beginDelimiter || '{{';
me._endDelimiter = endDelimiter || '}}';
handlebarsDelimiters(Handlebars, [me._beginDelimiter, me._endDelimiter]);
return me.engine;
this.engine.detect = function (body) {
return body.indexOf('{{') > -1;
};

this.engine = function (source, data) {
return Handlebars.compile(source)(data);
// register underscore.string helpers
options.fns = options.fns || require('underscore.string');

var registerHelpers = function (fns) {
Object.keys(fns)
.filter(function (fnName) {
return Handlebars.Utils.isFunction(fns[fnName]);
})
.forEach(function (fnName) {
Handlebars.registerHelper(fnName, function () {
var args = Array.prototype.slice.call(arguments, 0).slice(0, -1);
return fns[fnName].apply(this, args);
});
});
};

this.engine.detect = function(body) {
return body.indexOf(me._beginDelimiter) > -1;
};
var registerPartials = function (partialsPath) {
fs.readdirSync(partialsPath).forEach(function (p) {
var template = fs.readFileSync(partialsPath + p, 'utf-8');
Handlebars.registerPartial(p, template);
});
};

return this.setDelimiter(beginDelimiter, endDelimiter);
if(options && options.fns) registerHelpers(options.fns);
if(options && options.partials) registerPartials(options.partials);
return this.engine;
};
3 changes: 1 addition & 2 deletions package.json
Expand Up @@ -22,8 +22,7 @@
"bugs": "https://github.com/hurrymaplelad/yeoman-handlebars-engine/issues",
"dependencies": {
"underscore.string": "^2.3.3",
"handlebars": "^2.0.0",
"handlebars-delimiters": "^0.1.0"
"handlebars": "^2.0.0"
},
"devDependencies": {
"coffee-script": ">=1.7.x",
Expand Down