From e2e8f168458cb54854e1cac58c9ab5ed238a67b5 Mon Sep 17 00:00:00 2001 From: Chris Wanstrath Date: Sun, 18 Apr 2010 10:48:54 -0400 Subject: [PATCH] Higher Order Sections If a section key returns a function, it will be called and passed both the unrendered block of text and a renderer convenience function. Given this JS: "name": "Tater", "bolder": function() { return function(text, render) { return "" + render(text) + '' } } And this template: {{#bolder}}Hi {{name}}.{{/bolder}} We'll get this output: Hi Tater. As you can see, we're pre-processing the text in the block. This can be used to implement caching, filters (like syntax highlighting), etc. You can use `this.name` to access the attribute `name` from your view. --- CHANGES.md | 1 + README.md | 28 ++++++++++++++++++++++++++++ examples/higher_order_sections.html | 1 + examples/higher_order_sections.js | 9 +++++++++ examples/higher_order_sections.txt | 1 + mustache.js | 5 +++++ 6 files changed, 45 insertions(+) create mode 100644 examples/higher_order_sections.html create mode 100644 examples/higher_order_sections.js create mode 100644 examples/higher_order_sections.txt diff --git a/CHANGES.md b/CHANGES.md index 7134ffc..bd88ae9 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,6 +1,7 @@ # mustache.js Changes ## 0.3.0 (??-??-????) +* Added higher order sections. ## 0.2.3 (28-03-2010) diff --git a/README.md b/README.md index 0eea4e2..7c1e8e0 100644 --- a/README.md +++ b/README.md @@ -88,6 +88,34 @@ the items. Use `{{.}}` to access the current item inside the enumeration section Joe's shopping card: +### Higher Order Sections + +If a section key returns a function, it will be called and passed both the unrendered +block of text and a renderer convenience function. + +Given this JS: + + "name": "Tater", + "bolder": function() { + return function(text, render) { + return "" + render(text) + '' + } + } + +And this template: + + {{#bolder}}Hi {{name}}.{{/bolder}} + +We'll get this output: + + Hi Tater. + +As you can see, we're pre-processing the text in the block. This can be used to +implement caching, filters (like syntax highlighting), etc. + +You can use `this.name` to access the attribute `name` from your view. + + ### View Partials mustache.js supports a quite powerful but yet simple view partial mechanism. Use the diff --git a/examples/higher_order_sections.html b/examples/higher_order_sections.html new file mode 100644 index 0000000..04f5318 --- /dev/null +++ b/examples/higher_order_sections.html @@ -0,0 +1 @@ +{{#bolder}}Hi {{name}}.{{/bolder}} diff --git a/examples/higher_order_sections.js b/examples/higher_order_sections.js new file mode 100644 index 0000000..c7e558e --- /dev/null +++ b/examples/higher_order_sections.js @@ -0,0 +1,9 @@ +var higher_order_sections = { + "name": "Tater", + "helper": "To tinker?", + "bolder": function() { + return function(text, render) { + return "" + render(text) + ' ' + this.helper; + } + } +} \ No newline at end of file diff --git a/examples/higher_order_sections.txt b/examples/higher_order_sections.txt new file mode 100644 index 0000000..9db786a --- /dev/null +++ b/examples/higher_order_sections.txt @@ -0,0 +1 @@ +Hi Tater. To tinker? diff --git a/mustache.js b/mustache.js index 3ff899c..4a73e22 100644 --- a/mustache.js +++ b/mustache.js @@ -109,6 +109,11 @@ var Mustache = function() { return that.render(content, that.merge(context, that.create_context(row)), partials, true); }).join(""); + } else if(typeof value === "function") { + // higher order section + return value.call(context, content, function(text) { + return that.render(text, context, partials, true); + }) } else if(value) { // boolean section return that.render(content, context, partials, true); } else {