Kaynağa Gözat

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

tags/0.3.0
Jan Lehnardt 16 yıl önce
ebeveyn
işleme
a6de1a3bdd
6 değiştirilmiş dosya ile 60 ekleme ve 22 silme
  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 Dosyayı Görüntüle

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

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

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


+ 21
- 0
README.md Dosyayı Görüntüle

@@ -150,6 +150,27 @@ Here is the result:
</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

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


+ 2
- 0
examples/inverted_section.html Dosyayı Görüntüle

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

+ 3
- 0
examples/inverted_section.js Dosyayı Görüntüle

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

+ 1
- 0
examples/inverted_section.txt Dosyayı Görüntüle

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

+ 32
- 22
mustache.js Dosyayı Görüntüle

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

var that = this;
// 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...
return template.replace(regex, function(match, name, content) {
return template.replace(regex, function(match, type, name, content) {
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 new_regex = function() {
return new RegExp(that.otag + "(=|!|>|\\{|%)?([^\/#]+?)\\1?" +
return new RegExp(that.otag + "(=|!|>|\\{|%)?([^\/#\^]+?)\\1?" +
that.ctag + "+", "g");
};



Yükleniyor…
İptal
Kaydet