From 1fa8421678730692cb9a8ee1358211fd57edb396 Mon Sep 17 00:00:00 2001 From: Jan Lehnardt Date: Sat, 14 Nov 2009 14:41:42 +0100 Subject: [PATCH 1/4] make strings of arrays a pragma option, introduce {{%PRAGMA}} tag to enable features --- examples/array_of_strings.html | 1 + examples/array_of_strings.txt | 1 + examples/pragma.html | 1 + examples/pragma.js | 3 +++ examples/pragma.txt | 2 ++ mustache.js | 26 ++++++++++++++++++++++++-- 6 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 examples/pragma.html create mode 100644 examples/pragma.js create mode 100644 examples/pragma.txt diff --git a/examples/array_of_strings.html b/examples/array_of_strings.html index 38fa5ab..56480d9 100644 --- a/examples/array_of_strings.html +++ b/examples/array_of_strings.html @@ -1 +1,2 @@ +{{%ENABLE-STRING-ARRAYS}} {{#array_of_strings}} {{.}} {{/array_of_strings}} \ No newline at end of file diff --git a/examples/array_of_strings.txt b/examples/array_of_strings.txt index 4a1f475..b20df17 100644 --- a/examples/array_of_strings.txt +++ b/examples/array_of_strings.txt @@ -1 +1,2 @@ + hello world diff --git a/examples/pragma.html b/examples/pragma.html new file mode 100644 index 0000000..ccccc93 --- /dev/null +++ b/examples/pragma.html @@ -0,0 +1 @@ +{{%PRAGMA}} diff --git a/examples/pragma.js b/examples/pragma.js new file mode 100644 index 0000000..b9eb6e7 --- /dev/null +++ b/examples/pragma.js @@ -0,0 +1,3 @@ +var pragma = { + foo: 1 +}; diff --git a/examples/pragma.txt b/examples/pragma.txt new file mode 100644 index 0000000..139597f --- /dev/null +++ b/examples/pragma.txt @@ -0,0 +1,2 @@ + + diff --git a/mustache.js b/mustache.js index c01f0eb..37284fc 100644 --- a/mustache.js +++ b/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["ENABLE-STRING-ARRAYS"]) { return {'.': _context}; } }, From 7cbb2bd52598fe67e7747bf9219cb837e9b74b35 Mon Sep 17 00:00:00 2001 From: Jan Lehnardt Date: Sat, 14 Nov 2009 14:41:57 +0100 Subject: [PATCH 2/4] add change log --- CHANGES.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 CHANGES.md diff --git a/CHANGES.md b/CHANGES.md new file mode 100644 index 0000000..7fb74b2 --- /dev/null +++ b/CHANGES.md @@ -0,0 +1,6 @@ +# Mustache.js Changes + +## 0.2 (??-??-????) + +* ctemplate compat: Partials are indicated by >, not < + From b57940491203f7eb6743067fc47beb5b2551d739 Mon Sep 17 00:00:00 2001 From: Jan Lehnardt Date: Sat, 14 Nov 2009 14:43:38 +0100 Subject: [PATCH 3/4] s/ENABLE-STRING-ARRAYS/JSTACHE-ENABLE-STRING-ARRAYS/ --- CHANGES.md | 3 ++- examples/array_of_strings.html | 2 +- mustache.js | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 7fb74b2..3967192 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -3,4 +3,5 @@ ## 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}} diff --git a/examples/array_of_strings.html b/examples/array_of_strings.html index 56480d9..dcd1c92 100644 --- a/examples/array_of_strings.html +++ b/examples/array_of_strings.html @@ -1,2 +1,2 @@ -{{%ENABLE-STRING-ARRAYS}} +{{%JSTACHE-ENABLE-STRING-ARRAYS}} {{#array_of_strings}} {{.}} {{/array_of_strings}} \ No newline at end of file diff --git a/mustache.js b/mustache.js index 37284fc..20d3f43 100644 --- a/mustache.js +++ b/mustache.js @@ -202,7 +202,7 @@ var Mustache = function() { create_context: function(_context) { if(this.is_object(_context)) { return _context; - } else if(this.pragmas["ENABLE-STRING-ARRAYS"]) { + } else if(this.pragmas["JSTACHE-ENABLE-STRING-ARRAYS"]) { return {'.': _context}; } }, From a9cb27980bb1f32d0d85249063b996c2056c94b8 Mon Sep 17 00:00:00 2001 From: Jan Lehnardt Date: Sat, 14 Nov 2009 14:44:23 +0100 Subject: [PATCH 4/4] pedantery --- CHANGES.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 3967192..b091054 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -2,6 +2,6 @@ ## 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}} +* 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}}`.