Quellcode durchsuchen

make strings of arrays a pragma option, introduce {{%PRAGMA}} tag to enable features

tags/0.2
Jan Lehnardt vor 16 Jahren
Ursprung
Commit
1fa8421678
6 geänderte Dateien mit 32 neuen und 2 gelöschten Zeilen
  1. +1
    -0
      examples/array_of_strings.html
  2. +1
    -0
      examples/array_of_strings.txt
  3. +1
    -0
      examples/pragma.html
  4. +3
    -0
      examples/pragma.js
  5. +2
    -0
      examples/pragma.txt
  6. +24
    -2
      mustache.js

+ 1
- 0
examples/array_of_strings.html Datei anzeigen

@@ -1 +1,2 @@
{{%ENABLE-STRING-ARRAYS}}
{{#array_of_strings}} {{.}} {{/array_of_strings}} {{#array_of_strings}} {{.}} {{/array_of_strings}}

+ 1
- 0
examples/array_of_strings.txt Datei anzeigen

@@ -1 +1,2 @@

hello world hello world

+ 1
- 0
examples/pragma.html Datei anzeigen

@@ -0,0 +1 @@
{{%PRAGMA}}

+ 3
- 0
examples/pragma.js Datei anzeigen

@@ -0,0 +1,3 @@
var pragma = {
foo: 1
};

+ 2
- 0
examples/pragma.txt Datei anzeigen

@@ -0,0 +1,2 @@



+ 24
- 2
mustache.js Datei anzeigen

@@ -14,6 +14,7 @@ var Mustache = function() {
Renderer.prototype = { Renderer.prototype = {
otag: "{{", otag: "{{",
ctag: "}}", ctag: "}}",
pragmas: {},


render: function(template, context, partials) { render: function(template, context, partials) {
// fail fast // fail fast
@@ -21,10 +22,29 @@ var Mustache = function() {
return template; return template;
} }


template = this.render_pragmas(template);
var html = this.render_section(template, context, partials); var html = this.render_section(template, context, partials);
return this.render_tags(html, context, partials); return this.render_tags(html, context, partials);
}, },


/*
Looks for %PRAGMAS
*/
render_pragmas: function(template) {
// no pragmas
if(template.indexOf(this.otag + "%") == -1) {
return template;
}

var that = this;
var regex = new RegExp(this.otag + "%(.+)" + this.ctag);
return template.replace(regex, function(match, pragma) {
that.pragmas[pragma] = true;
return "";
// ignore unknown pragmas silently
});
},

/* /*
Tries to find a partial in the global scope and render it Tries to find a partial in the global scope and render it
*/ */
@@ -73,7 +93,7 @@ var Mustache = function() {
var lines = template.split("\n"); var lines = template.split("\n");


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");
}; };


@@ -97,6 +117,7 @@ var Mustache = function() {
return that.render_partial(name, context, partials); return that.render_partial(name, context, partials);
case "{": // the triple mustache is unescaped case "{": // the triple mustache is unescaped
return that.find(name, context); return that.find(name, context);
return "";
default: // escape the value default: // escape the value
return that.escape(that.find(name, context)); return that.escape(that.find(name, context));
} }
@@ -177,10 +198,11 @@ var Mustache = function() {
return _new; return _new;
}, },


// by @langalex, support for arrays of strings
create_context: function(_context) { create_context: function(_context) {
if(this.is_object(_context)) { if(this.is_object(_context)) {
return _context; return _context;
} else {
} else if(this.pragmas["ENABLE-STRING-ARRAYS"]) {
return {'.': _context}; return {'.': _context};
} }
}, },


Laden…
Abbrechen
Speichern