| @@ -0,0 +1,2 @@ | |||||
| Hi {{name}}. | |||||
| Hi {{ucase name}}. | |||||
| @@ -0,0 +1,8 @@ | |||||
| var helpers_helpers = { | |||||
| ucase: function(s) {return s.toUpperCase(); } | |||||
| }; | |||||
| var helpers = { | |||||
| name: "Chris" | |||||
| }; | |||||
| @@ -0,0 +1,2 @@ | |||||
| Hi Chris. | |||||
| Hi CHRIS. | |||||
| @@ -0,0 +1,3 @@ | |||||
| Hi {{name}}. | |||||
| Hi {{ucase name}}. | |||||
| Hi {{lcase name}}. | |||||
| @@ -0,0 +1,8 @@ | |||||
| var helpers_error_helpers = { | |||||
| ucase: function(s) {return s.toUpperCase(); } | |||||
| }; | |||||
| var helpers_error = { | |||||
| name: "Chris" | |||||
| }; | |||||
| @@ -0,0 +1 @@ | |||||
| ERROR: Helper 'lcase' is not a registered helper | |||||
| @@ -13,6 +13,7 @@ var Mustache = function() { | |||||
| ctag: "}}", | ctag: "}}", | ||||
| pragmas: {}, | pragmas: {}, | ||||
| buffer: [], | buffer: [], | ||||
| helper_functions: {}, | |||||
| pragmas_implemented: { | pragmas_implemented: { | ||||
| "IMPLICIT-ITERATOR": true | "IMPLICIT-ITERATOR": true | ||||
| }, | }, | ||||
| @@ -269,7 +270,22 @@ var Mustache = function() { | |||||
| } | } | ||||
| var value; | 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]; | |||||
| } | |||||
| } | |||||
| // check for dot notation eg. foo.bar | // check for dot notation eg. foo.bar | ||||
| if(name.match(/([a-z_]+)\./ig)){ | if(name.match(/([a-z_]+)\./ig)){ | ||||
| var childValue = this.walk_context(name, context); | var childValue = this.walk_context(name, context); | ||||
| @@ -286,8 +302,13 @@ var Mustache = function() { | |||||
| } | } | ||||
| if(typeof value === "function") { | if(typeof value === "function") { | ||||
| return value.apply(context); | |||||
| value = value.apply(context); | |||||
| } | } | ||||
| if(helper) { | |||||
| value = helper.apply(context, [value]); | |||||
| } | |||||
| if(value !== undefined) { | if(value !== undefined) { | ||||
| return value; | return value; | ||||
| } | } | ||||
| @@ -408,11 +429,14 @@ var Mustache = function() { | |||||
| /* | /* | ||||
| Turns a template and view into HTML | 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) { | if(send_fun) { | ||||
| renderer.send = send_fun; | renderer.send = send_fun; | ||||
| } | } | ||||
| if(helpers) { | |||||
| renderer.helper_functions = helpers; | |||||
| } | |||||
| renderer.render(template, view || {}, partials); | renderer.render(template, view || {}, partials); | ||||
| if(!send_fun) { | if(!send_fun) { | ||||
| return renderer.buffer.join("\n"); | return renderer.buffer.join("\n"); | ||||
| @@ -84,9 +84,10 @@ describe "mustache" do | |||||
| runner = <<-JS | runner = <<-JS | ||||
| try { | try { | ||||
| #{@boilerplate} | #{@boilerplate} | ||||
| var #{testname}_helpers = null; | |||||
| #{view} | #{view} | ||||
| var template = #{template}; | var template = #{template}; | ||||
| var result = Mustache.to_html(template, #{testname}); | |||||
| var result = Mustache.to_html(template, #{testname}, null, null, #{testname}_helpers); | |||||
| print(result); | print(result); | ||||
| } catch(e) { | } catch(e) { | ||||
| print('ERROR: ' + e.message); | print('ERROR: ' + e.message); | ||||
| @@ -102,6 +103,7 @@ describe "mustache" do | |||||
| runner = <<-JS | runner = <<-JS | ||||
| try { | try { | ||||
| #{@boilerplate} | #{@boilerplate} | ||||
| var #{testname}_helpers = null; | |||||
| #{view} | #{view} | ||||
| var chunks = []; | var chunks = []; | ||||
| var sendFun = function(chunk) { | var sendFun = function(chunk) { | ||||
| @@ -110,7 +112,7 @@ describe "mustache" do | |||||
| } | } | ||||
| } | } | ||||
| var template = #{template}; | var template = #{template}; | ||||
| Mustache.to_html(template, #{testname}, null, sendFun); | |||||
| Mustache.to_html(template, #{testname}, null, sendFun, #{testname}_helpers); | |||||
| print(chunks.join("\\n")); | print(chunks.join("\\n")); | ||||
| } catch(e) { | } catch(e) { | ||||
| print('ERROR: ' + e.message); | print('ERROR: ' + e.message); | ||||
| @@ -132,10 +134,11 @@ describe "mustache" do | |||||
| runner = <<-JS | runner = <<-JS | ||||
| try { | try { | ||||
| #{@boilerplate} | #{@boilerplate} | ||||
| var #{testname}_helpers = null; | |||||
| #{view} | #{view} | ||||
| var template = #{template}; | var template = #{template}; | ||||
| var partials = {"partial": #{partial}}; | 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); | print(result); | ||||
| } catch(e) { | } catch(e) { | ||||
| print('ERROR: ' + e.message); | print('ERROR: ' + e.message); | ||||
| @@ -152,7 +155,8 @@ describe "mustache" do | |||||
| runner = <<-JS | runner = <<-JS | ||||
| try { | try { | ||||
| #{@boilerplate} | #{@boilerplate} | ||||
| #{view}; | |||||
| var #{testname}_helpers = null; | |||||
| #{view} | |||||
| var template = #{template}; | var template = #{template}; | ||||
| var partials = {"partial": #{partial}}; | var partials = {"partial": #{partial}}; | ||||
| var chunks = []; | var chunks = []; | ||||
| @@ -161,7 +165,7 @@ describe "mustache" do | |||||
| chunks.push(chunk); | 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")); | print(chunks.join("\\n")); | ||||
| } catch(e) { | } catch(e) { | ||||
| print('ERROR: ' + e.message); | print('ERROR: ' + e.message); | ||||