Skip to content

Implement Helpers. #139

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

Open
wants to merge 3 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions examples/helpers.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Hi {{name}}.
Hi {{name|ucase}}.
8 changes: 8 additions & 0 deletions examples/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
var helpers_helpers = {
ucase: function(s) {return s.toUpperCase(); }
};

var helpers = {
name: "Chris"
};

2 changes: 2 additions & 0 deletions examples/helpers.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Hi Chris.
Hi CHRIS.
3 changes: 3 additions & 0 deletions examples/helpers_error.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Hi {{name}}.
Hi {{name|ucase}}.
Hi {{name|lcase}}.
8 changes: 8 additions & 0 deletions examples/helpers_error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
var helpers_error_helpers = {
ucase: function(s) {return s.toUpperCase(); }
};

var helpers_error = {
name: "Chris"
};

1 change: 1 addition & 0 deletions examples/helpers_error.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ERROR: Helper 'lcase' is not a registered helper
2 changes: 2 additions & 0 deletions examples/helpers_pipe.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Hi {{name}}.
Hi {{name|ucase|bangify}}.
9 changes: 9 additions & 0 deletions examples/helpers_pipe.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
var helpers_pipe_helpers = {
ucase: function(s) {return s.toUpperCase(); },
bangify: function(s) {return "!" + s + "!"}
};

var helpers_pipe = {
name: "Chris"
};

2 changes: 2 additions & 0 deletions examples/helpers_pipe.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Hi Chris.
Hi !CHRIS!.
31 changes: 27 additions & 4 deletions mustache.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ var Mustache = function() {
ctag: "}}",
pragmas: {},
buffer: [],
helper_functions: {},
pragmas_implemented: {
"IMPLICIT-ITERATOR": true
},
Expand Down Expand Up @@ -269,7 +270,12 @@ var Mustache = function() {
}

var value;

var helpers;

// check for helper e.g. name = "name|helperfun"
var helpers = name.split("|");
name = helpers.shift();

// check for dot notation eg. foo.bar
if(name.match(/([a-z_]+)\./ig)){
var childValue = this.walk_context(name, context);
Expand All @@ -286,8 +292,22 @@ var Mustache = function() {
}

if(typeof value === "function") {
return value.apply(context);
value = value.apply(context);
}

var number_of_helpers = helpers.length;
if(number_of_helpers > 0) {
for(var idx = 0; idx < number_of_helpers; idx++) {
var helper = helpers[idx];
if(!this.helper_functions[helper]
|| typeof this.helper_functions[helper] != "function") {
throw {message:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

throwing a new Error("....") is generally better. It captures a stack trace, and is more consistent.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

throw {} is consistent with the rest of the code, so I left it so far. Once this is merged, we can look into improving the rest as well.

"Helper '" + helper + "' is not a registered helper"};
}
value = this.helper_functions[helper].apply(context, [value]);
}
}

if(value !== undefined) {
return value;
}
Expand Down Expand Up @@ -408,11 +428,14 @@ var Mustache = function() {
/*
Turns a template and view into HTML
*/
to_html: function(template, view, partials, send_fun) {
var renderer = new Renderer();
to_html: function(template, view, partials, send_fun, helpers) {
var renderer = new Renderer(helpers);
if(send_fun) {
renderer.send = send_fun;
}
if(helpers) {
renderer.helper_functions = helpers;
}
renderer.render(template, view || {}, partials);
if(!send_fun) {
return renderer.buffer.join("\n");
Expand Down
14 changes: 9 additions & 5 deletions test/mustache_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,10 @@ def load_test(dir, name, partial=false)
runner = <<-JS
try {
#{@boilerplate}
var #{testname}_helpers = null;
#{view}
var template = #{template};
var result = Mustache.to_html(template, #{testname});
var result = Mustache.to_html(template, #{testname}, null, null, #{testname}_helpers);
print(result);
} catch(e) {
print('ERROR: ' + e.message);
Expand All @@ -102,6 +103,7 @@ def load_test(dir, name, partial=false)
runner = <<-JS
try {
#{@boilerplate}
var #{testname}_helpers = null;
#{view}
var chunks = [];
var sendFun = function(chunk) {
Expand All @@ -110,7 +112,7 @@ def load_test(dir, name, partial=false)
}
}
var template = #{template};
Mustache.to_html(template, #{testname}, null, sendFun);
Mustache.to_html(template, #{testname}, null, sendFun, #{testname}_helpers);
print(chunks.join("\\n"));
} catch(e) {
print('ERROR: ' + e.message);
Expand All @@ -132,10 +134,11 @@ def load_test(dir, name, partial=false)
runner = <<-JS
try {
#{@boilerplate}
var #{testname}_helpers = null;
#{view}
var template = #{template};
var partials = {"partial": #{partial}};
var result = Mustache.to_html(template, partial_context, partials);
var result = Mustache.to_html(template, partial_context, partials, null, #{testname}_helpers);
print(result);
} catch(e) {
print('ERROR: ' + e.message);
Expand All @@ -152,7 +155,8 @@ def load_test(dir, name, partial=false)
runner = <<-JS
try {
#{@boilerplate}
#{view};
var #{testname}_helpers = null;
#{view}
var template = #{template};
var partials = {"partial": #{partial}};
var chunks = [];
Expand All @@ -161,7 +165,7 @@ def load_test(dir, name, partial=false)
chunks.push(chunk);
}
}
Mustache.to_html(template, partial_context, partials, sendFun);
Mustache.to_html(template, partial_context, partials, sendFun, #{testname}_helpers);
print(chunks.join("\\n"));
} catch(e) {
print('ERROR: ' + e.message);
Expand Down