From 1fa8421678730692cb9a8ee1358211fd57edb396 Mon Sep 17 00:00:00 2001 From: Jan Lehnardt Date: Sat, 14 Nov 2009 14:41:42 +0100 Subject: [PATCH] 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}; } },