Procházet zdrojové kódy

add inverted sections. Closes #17 and #24. Patch by defunkt.

tags/0.3.0
Jan Lehnardt před 16 roky
rodič
revize
a6de1a3bdd
6 změnil soubory, kde provedl 60 přidání a 22 odebrání
  1. +1
    -0
      CHANGES.md
  2. +21
    -0
      README.md
  3. +2
    -0
      examples/inverted_section.html
  4. +3
    -0
      examples/inverted_section.js
  5. +1
    -0
      examples/inverted_section.txt
  6. +32
    -22
      mustache.js

+ 1
- 0
CHANGES.md Zobrazit soubor

@@ -2,6 +2,7 @@


## 0.3.0 (??-??-????) ## 0.3.0 (??-??-????)


* Added inverted sections.
* Avoid double encoding of entities. * Avoid double encoding of entities.
* Use sections to dereference subcontexts. * Use sections to dereference subcontexts.
* Added higher order sections. * Added higher order sections.


+ 21
- 0
README.md Zobrazit soubor

@@ -150,6 +150,27 @@ Here is the result:
</ul> </ul>




### Inverted Sections

An inverted section opens with `{{^section}}` instead of `{{#section}}` and uses a
boolean negative to evaluate. Empty arrays are considered falsy.

View:

var inverted_section = {
"repo": []
}

Template:

{{#repo}}<b>{{name}}</b>{{/repo}}
{{^repo}}No repos :({{/repo}}

Result:

No repos :(


### View Partials ### View Partials


mustache.js supports a quite powerful but yet simple view partial mechanism. Use the mustache.js supports a quite powerful but yet simple view partial mechanism. Use the


+ 2
- 0
examples/inverted_section.html Zobrazit soubor

@@ -0,0 +1,2 @@
{{#repo}}<b>{{name}}</b>{{/repo}}
{{^repo}}No repos :({{/repo}}

+ 3
- 0
examples/inverted_section.js Zobrazit soubor

@@ -0,0 +1,3 @@
var inverted_section = {
"repo": []
}

+ 1
- 0
examples/inverted_section.txt Zobrazit soubor

@@ -0,0 +1 @@
No repos :(

+ 32
- 22
mustache.js Zobrazit soubor

@@ -93,35 +93,45 @@ var Mustache = function() {
Renders inverted (^) and normal (#) sections Renders inverted (^) and normal (#) sections
*/ */
render_section: function(template, context, partials) { render_section: function(template, context, partials) {
if(!this.includes("#", template)) {
if(!this.includes("#", template) && !this.includes("^", template)) {
return template; return template;
} }


var that = this; var that = this;
// CSW - Added "+?" so it finds the tighest bound, not the widest // CSW - Added "+?" so it finds the tighest bound, not the widest
var regex = new RegExp(this.otag + "\\#(.+)" + this.ctag +
"\\s*([\\s\\S]+?)" + this.otag + "\\/\\1" + this.ctag + "\\s*", "mg");
var regex = new RegExp(this.otag + "(\\^|\\#)(.+)" + this.ctag +
"\\s*([\\s\\S]+?)" + this.otag + "\\/\\2" + this.ctag +
"\\s*", "mg");


// for each {{#foo}}{{/foo}} section do... // for each {{#foo}}{{/foo}} section do...
return template.replace(regex, function(match, name, content) {
return template.replace(regex, function(match, type, name, content) {
var value = that.find(name, context); var value = that.find(name, context);
if(that.is_array(value)) { // Enumerable, Let's loop!
return that.map(value, function(row) {
return that.render(content, that.merge(context,
that.create_context(row)), partials, true);
}).join("");
} else if(that.is_object(value)) { // Object, Use it as subcontext!
return that.render(content,
that.merge(context, that.create_context(value)), partials, true);
} 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 {
return "";
if(type == "^") { // inverted section
if(!value || that.is_array(value) && value.length == 0) {
// false or empty list, render it
return that.render(content, context, partials, true);
} else {
return "";
}
} else if(type == "#") { // normal section
if(that.is_array(value)) { // Enumerable, Let's loop!
return that.map(value, function(row) {
return that.render(content, that.merge(context,
that.create_context(row)), partials, true);
}).join("");
} else if(that.is_object(value)) { // Object, Use it as subcontext!
return that.render(content,
that.merge(context, that.create_context(value)), partials, true);
} 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 {
return "";
}
} }
}); });
}, },
@@ -134,7 +144,7 @@ var Mustache = function() {
var that = this; var that = this;


var new_regex = function() { var new_regex = function() {
return new RegExp(that.otag + "(=|!|>|\\{|%)?([^\/#]+?)\\1?" +
return new RegExp(that.otag + "(=|!|>|\\{|%)?([^\/#\^]+?)\\1?" +
that.ctag + "+", "g"); that.ctag + "+", "g");
}; };




Načítá se…
Zrušit
Uložit