diff --git a/README.md b/README.md index 1e6d21a..caeb3e5 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,15 @@ handlebars.nested [![Build Status](https://travis-ci.org/mateusmaso/handlebars.nested.svg?branch=master)](https://travis-ci.org/mateusmaso/handlebars.nested) ================= -This library is an extension for Handlebars which allows nesting helpers or expressions within other helpers in one level deep. It was first created as a workaround to older versions, but after more issues came across it turned out to be an effective solution for those problems. +This library is an extension for Handlebars which allows nesting helpers or expressions within other helpers in one level deep. It was first created as a workaround to older versions, but after more issues it turned out to be an effective solution for many problems. ## Features -* Nesting helpers in one level deep. -* Nesting expressions with helpers in one level deep. +* Nesting helpers and expressions in one level deep. + +## Dependencies + +* handlebars.js (>= 1.0) ## Examples diff --git a/dist/handlebars.nested.js b/dist/handlebars.nested.js index f0cb18a..9b07d74 100644 --- a/dist/handlebars.nested.js +++ b/dist/handlebars.nested.js @@ -19,41 +19,36 @@ }(this, function(Handlebars) { + var Utils = Handlebars.Utils; var registerHelper = Handlebars.registerHelper; - var isString = function(object) { + Handlebars.Utils.isString = function(object) { return toString.call(object) == '[object String]'; }; Handlebars.registerHelper = function(name, fn, inverse) { var nestedFn = function() { - var args = []; + var nestedArguments = []; for (var index = 0; index < arguments.length; index++) { var argument = arguments[index]; if (argument && argument.hash) { - for (key in argument.hash) { - argument.hash[key] = Handlebars.resolveNested.apply(this, [argument.hash[key]]); - } - - args.push(argument); + for (key in argument.hash) argument.hash[key] = Handlebars.resolveNested.apply(this, [argument.hash[key]]); + nestedArguments.push(argument); } else { - args.push(Handlebars.resolveNested.apply(this, [argument])); + nestedArguments.push(Handlebars.resolveNested.apply(this, [argument])); } } - return fn.apply(this, args); + return fn.apply(this, nestedArguments); }; registerHelper.apply(this, [name, nestedFn, inverse]); }; Handlebars.resolveNested = function(value) { - if (isString(value) && value.indexOf('{{') >= 0) { - value = Handlebars.compile(value)(this); - } - + if (Utils.isString(value) && value.indexOf('{{') >= 0) value = Handlebars.compile(value)(this); return value; }; diff --git a/dist/handlebars.nested.min.js b/dist/handlebars.nested.min.js index f2b831c..e7302dc 100644 --- a/dist/handlebars.nested.min.js +++ b/dist/handlebars.nested.min.js @@ -7,4 +7,4 @@ // // http://github.com/mateusmaso/handlebars.nested -!function(a,b){"undefined"!=typeof exports?("undefined"!=typeof module&&module.exports&&(module.exports=b(global.Handlebars)),exports=b(global.Handlebars)):b(a.Handlebars)}(this,function(a){var b=a.registerHelper,c=function(a){return"[object String]"==toString.call(a)};a.registerHelper=function(c,d,e){var f=function(){for(var b=[],c=0;c=0&&(b=a.compile(b)(this)),b}}); \ No newline at end of file +!function(a,b){"undefined"!=typeof exports?("undefined"!=typeof module&&module.exports&&(module.exports=b(global.Handlebars)),exports=b(global.Handlebars)):b(a.Handlebars)}(this,function(a){var b=a.Utils,c=a.registerHelper;a.Utils.isString=function(a){return"[object String]"==toString.call(a)},a.registerHelper=function(b,d,e){var f=function(){for(var b=[],c=0;c=0&&(c=a.compile(c)(this)),c}}); \ No newline at end of file diff --git a/src/handlebars.nested.js b/src/handlebars.nested.js index d64aa54..e6315dc 100644 --- a/src/handlebars.nested.js +++ b/src/handlebars.nested.js @@ -10,41 +10,36 @@ }(this, function(Handlebars) { + var Utils = Handlebars.Utils; var registerHelper = Handlebars.registerHelper; - var isString = function(object) { + Handlebars.Utils.isString = function(object) { return toString.call(object) == '[object String]'; }; Handlebars.registerHelper = function(name, fn, inverse) { var nestedFn = function() { - var args = []; + var nestedArguments = []; for (var index = 0; index < arguments.length; index++) { var argument = arguments[index]; if (argument && argument.hash) { - for (key in argument.hash) { - argument.hash[key] = Handlebars.resolveNested.apply(this, [argument.hash[key]]); - } - - args.push(argument); + for (key in argument.hash) argument.hash[key] = Handlebars.resolveNested.apply(this, [argument.hash[key]]); + nestedArguments.push(argument); } else { - args.push(Handlebars.resolveNested.apply(this, [argument])); + nestedArguments.push(Handlebars.resolveNested.apply(this, [argument])); } } - return fn.apply(this, args); + return fn.apply(this, nestedArguments); }; registerHelper.apply(this, [name, nestedFn, inverse]); }; Handlebars.resolveNested = function(value) { - if (isString(value) && value.indexOf('{{') >= 0) { - value = Handlebars.compile(value)(this); - } - + if (Utils.isString(value) && value.indexOf('{{') >= 0) value = Handlebars.compile(value)(this); return value; };