Skip to content

Commit

Permalink
Higher Order Sections
Browse files Browse the repository at this point in the history
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 "<b>" + render(text) + '</b>'
    }
  }

And this template:

  {{#bolder}}Hi {{name}}.{{/bolder}}

We'll get this output:

  <b>Hi Tater.</b>

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.
  • Loading branch information
defunkt authored and janl committed Apr 26, 2010
1 parent b76120a commit e2e8f16
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# mustache.js Changes

## 0.3.0 (??-??-????)
* Added higher order sections.


## 0.2.3 (28-03-2010)
Expand Down
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,34 @@ the items. Use `{{.}}` to access the current item inside the enumeration section
Joe's shopping card: <ul><li>bananas</li><li>apples</li></ul>


### 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 "<b>" + render(text) + '</b>'
}
}

And this template:

{{#bolder}}Hi {{name}}.{{/bolder}}

We'll get this output:

<b>Hi Tater.</b>

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
Expand Down
1 change: 1 addition & 0 deletions examples/higher_order_sections.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{#bolder}}Hi {{name}}.{{/bolder}}
9 changes: 9 additions & 0 deletions examples/higher_order_sections.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
var higher_order_sections = {
"name": "Tater",
"helper": "To tinker?",
"bolder": function() {
return function(text, render) {
return "<b>" + render(text) + '</b> ' + this.helper;
}
}
}
1 change: 1 addition & 0 deletions examples/higher_order_sections.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<b>Hi Tater.</b> To tinker?
5 changes: 5 additions & 0 deletions mustache.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit e2e8f16

Please sign in to comment.