From faa9fc7712b8b04454f357712d907e6c3adfc58a Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 7 Jul 2010 23:26:50 -0400 Subject: [PATCH] add pragma support --- mustache.js | 89 ++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 74 insertions(+), 15 deletions(-) diff --git a/mustache.js b/mustache.js index 8737f8b..66a34ae 100644 --- a/mustache.js +++ b/mustache.js @@ -11,9 +11,13 @@ var Mustache = function() { var Renderer = function(send_func) { this.send_func = send_func; + + this.pragmas = {}; }; Renderer.prototype = { render: function(template, context, partials) { + template = this.parse_pragmas(template, '{{', '}}'); + var tokens = this.tokenize(template, '{{', '}}'); this.parse(this.createParserContext(tokens, partials, '{{', '}}'), [context]); @@ -61,6 +65,50 @@ var Mustache = function() { return cleaned_tokens; }, + /* + Looks for %PRAGMAS + */ + parse_pragmas: function(template, openTag, closeTag) { + /* includes tag */ + function includes(needle, haystack) { + return haystack.indexOf(openTag + needle) !== -1; + } + + // no pragmas, easy escape + if(!includes("%", template)) { + return template; + } + + var that = this; + var regex = new RegExp(this.escape_regex(openTag) + "%([\\w-]+)(\\s*)(.*?(?=" + this.escape_regex(closeTag) + "))" + this.escape_regex(closeTag)); + return template.replace(regex, function(match, pragma, space, suffix) { + var options = undefined; + + if (suffix.length>0) { + var optionPairs = suffix.split(','); + var scratch; + + options = {}; + for (var i=0, n=optionPairs.length; i=0; --i) { - value = this.find(name, contextStack[i]); - if (value) { - return value; - } + value = this.find(name, contextStack[contextStack.length-1]); + if (value) { return value; } + + if (contextStack.length>1) { + value = this.find(name, contextStack[0]); + if (value) { return value; } } return undefined; @@ -414,13 +473,13 @@ var Mustache = function() { .replace(/>/g,'>'); } - var result = this.find(key, contextStack[contextStack.length-1]); + var result = this.find_in_stack(key, contextStack); if (result!==undefined) { this.send_func(escapeHTML(result)); } }, render_unescaped_variable: function(key, contextStack) { - var result = this.find(key, contextStack[contextStack.length-1]); + var result = this.find_in_stack(key, contextStack); if (result!==undefined) { this.send_func(result); } @@ -430,7 +489,7 @@ var Mustache = function() { throw new ParserException('Unknown partial \'' + key + '\''); } - var res = this.find(key, contextStack[contextStack.length-1]); + var res = this.find_in_stack(key, contextStack); if (this.is_object(res)) { contextStack.push(res); } @@ -451,18 +510,18 @@ var Mustache = function() { return _context; } else { var iterator = '.'; - // NO IMPLICIT-ITERATOR implementation yet - //if(that.pragmas["IMPLICIT-ITERATOR"] && - // that.pragmas["IMPLICIT-ITERATOR"].iterator) { - // iterator = that.pragmas["IMPLICIT-ITERATOR"].iterator; - //} + + if(that.pragmas["IMPLICIT-ITERATOR"] && + that.pragmas["IMPLICIT-ITERATOR"].iterator) { + iterator = that.pragmas["IMPLICIT-ITERATOR"].iterator; + } var ctx = {}; ctx[iterator] = _context; return ctx; } } - var value = this.find(key, contextStack[contextStack.length-1]); + var value = this.find_in_stack(key, contextStack); var tokens; if (sectionType==='invertedSection') {