瀏覽代碼

Merge branch 'pragma-array'

* pragma-array:
  pedantery
  s/ENABLE-STRING-ARRAYS/JSTACHE-ENABLE-STRING-ARRAYS/
  add change log
  make strings of arrays a pragma option, introduce {{%PRAGMA}} tag to enable features
tags/0.2
Jan Lehnardt 16 年之前
父節點
當前提交
2f8512e091
共有 7 個文件被更改,包括 39 次插入2 次删除
  1. +7
    -0
      CHANGES.md
  2. +1
    -0
      examples/array_of_strings.html
  3. +1
    -0
      examples/array_of_strings.txt
  4. +1
    -0
      examples/pragma.html
  5. +3
    -0
      examples/pragma.js
  6. +2
    -0
      examples/pragma.txt
  7. +24
    -2
      mustache.js

+ 7
- 0
CHANGES.md 查看文件

@@ -0,0 +1,7 @@
# Mustache.js Changes

## 0.2 (??-??-????)

* ctemplate compat: Partials are indicated by >, not <.
* Add support for {{%PRAGMA}} to enable features.
* Made array of strings an option. Enable with `{{%JSTACHE-ENABLE-STRING-ARRAYS}}`.

+ 1
- 0
examples/array_of_strings.html 查看文件

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

+ 1
- 0
examples/array_of_strings.txt 查看文件

@@ -1 +1,2 @@

hello world

+ 1
- 0
examples/pragma.html 查看文件

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

+ 3
- 0
examples/pragma.js 查看文件

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

+ 2
- 0
examples/pragma.txt 查看文件

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



+ 24
- 2
mustache.js 查看文件

@@ -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["JSTACHE-ENABLE-STRING-ARRAYS"]) {
return {'.': _context};
}
},


Loading…
取消
儲存