diff --git a/examples/helpers.html b/examples/helpers.html index ad3d792..9233e6c 100644 --- a/examples/helpers.html +++ b/examples/helpers.html @@ -1,2 +1,2 @@ Hi {{name}}. -Hi {{ucase name}}. +Hi {{name|ucase}}. diff --git a/examples/helpers_error.html b/examples/helpers_error.html index 5a499cc..03a8e86 100644 --- a/examples/helpers_error.html +++ b/examples/helpers_error.html @@ -1,3 +1,3 @@ Hi {{name}}. -Hi {{ucase name}}. -Hi {{lcase name}}. +Hi {{name|ucase}}. +Hi {{name|lcase}}. diff --git a/examples/helpers_pipe.html b/examples/helpers_pipe.html new file mode 100644 index 0000000..6d71d40 --- /dev/null +++ b/examples/helpers_pipe.html @@ -0,0 +1,2 @@ +Hi {{name}}. +Hi {{name|ucase|bangify}}. diff --git a/examples/helpers_pipe.js b/examples/helpers_pipe.js new file mode 100644 index 0000000..4a361f9 --- /dev/null +++ b/examples/helpers_pipe.js @@ -0,0 +1,9 @@ +var helpers_pipe_helpers = { + ucase: function(s) {return s.toUpperCase(); }, + bangify: function(s) {return "!" + s + "!"} +}; + +var helpers_pipe = { + name: "Chris" +}; + diff --git a/examples/helpers_pipe.txt b/examples/helpers_pipe.txt new file mode 100644 index 0000000..cc5a1eb --- /dev/null +++ b/examples/helpers_pipe.txt @@ -0,0 +1,2 @@ +Hi Chris. +Hi !CHRIS!. diff --git a/mustache.js b/mustache.js index 44cf00d..fc6ce86 100644 --- a/mustache.js +++ b/mustache.js @@ -270,20 +270,13 @@ var Mustache = function() { } var value; - var helper; - - // check for helper e.g. name = "helperfun name" - var helper_name = name.split(" "); - if(helper_name.length == 2) { // we have a helper - helper = helper_name[0]; - name = helper_name[1]; - if(!this.helper_functions[helper] - || typeof this.helper_functions[helper] != "function") { - throw {message: - "Helper '" + helper + "' is not a registered helper"}; - } else { - helper = this.helper_functions[helper]; - } + var helpers; + + // check for helper e.g. name = "name|helperfun" + var helper_name = name.split("|"); + if(helper_name.length > 1) { // we have at least one helper + name = helper_name[0]; + helpers = helper_name.slice(1); } // check for dot notation eg. foo.bar @@ -305,8 +298,16 @@ var Mustache = function() { value = value.apply(context); } - if(helper) { - value = helper.apply(context, [value]); + if(helpers) { + for(var idx in helpers) { + var helper = helpers[idx]; + if(!this.helper_functions[helper] + || typeof this.helper_functions[helper] != "function") { + throw {message: + "Helper '" + helper + "' is not a registered helper"}; + } + value = this.helper_functions[helper].apply(context, [value]); + } } if(value !== undefined) {