Kaynağa Gözat

Updated the render_segment to allow for the same variable to be reused for multiple template segments. Added a map function that can be used if not available on array natively - ahem IE ahem

tags/0.2
Chris Williams 16 yıl önce
ebeveyn
işleme
68830cadff
4 değiştirilmiş dosya ile 41 ekleme ve 2 silme
  1. +8
    -0
      examples/reuse_of_enumerables.html
  2. +6
    -0
      examples/reuse_of_enumerables.js
  3. +8
    -0
      examples/reuse_of_enumerables.txt
  4. +19
    -2
      mustache.js

+ 8
- 0
examples/reuse_of_enumerables.html Dosyayı Görüntüle

@@ -0,0 +1,8 @@
{{#terms}}
{{name}}
{{index}}
{{/terms}}
{{#terms}}
{{name}}
{{index}}
{{/terms}}

+ 6
- 0
examples/reuse_of_enumerables.js Dosyayı Görüntüle

@@ -0,0 +1,6 @@
var reuse_of_enumerables = {
terms: [
{name: 't1', index: 0},
{name: 't2', index: 1},
]
};

+ 8
- 0
examples/reuse_of_enumerables.txt Dosyayı Görüntüle

@@ -0,0 +1,8 @@
t1
0
t2
1
t1
0
t2
1

+ 19
- 2
mustache.js Dosyayı Görüntüle

@@ -50,14 +50,15 @@ var Mustache = function() {
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");
"\\s*([\\s\\S]+?)" + this.otag + "\\/\\1" + this.ctag + "\\s*", "mg");

// for each {{#foo}}{{/foo}} section do...
return template.replace(regex, function(match, name, content) {
var value = that.find(name, context);
if(that.is_array(value)) { // Enumerable, Let's loop!
return value.map(function(row) {
return that.map(value, function(row) {
return that.render(content, that.merge(context, that.create_context(row)));
}).join('');
} else if(value) { // boolean section
@@ -206,6 +207,22 @@ var Mustache = function() {
*/
trim: function(s) {
return s.replace(/^\s*|\s*$/g, '');
},

/*
Why, why, why? Because IE. Cry, cry cry.
*/
map: function(array, fn) {
if (typeof array.map == "function") {
return array.map(fn)
} else {
var r = [];
var l = array.length;
for(i=0;i<l;i++) {
r.push(fn(array[i]));
}
return r;
}
}
};


Yükleniyor…
İptal
Kaydet