Explorar el Código

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

tags/0.2
Jan Lehnardt hace 16 años
padre
commit
1fa8421678
Se han modificado 6 ficheros con 32 adiciones y 2 borrados
  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 Ver fichero

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

+ 1
- 0
examples/array_of_strings.txt Ver fichero

@@ -1 +1,2 @@

hello world

+ 1
- 0
examples/pragma.html Ver fichero

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

+ 3
- 0
examples/pragma.js Ver fichero

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

+ 2
- 0
examples/pragma.txt Ver fichero

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



+ 24
- 2
mustache.js Ver fichero

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

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

template = this.render_pragmas(template);
var html = this.render_section(template, 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
*/
@@ -73,7 +93,7 @@ var Mustache = function() {
var lines = template.split("\n");

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

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

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


Cargando…
Cancelar
Guardar