front_idill/lib/mustache.js
author bastiena
Thu, 24 May 2012 10:30:05 +0200
changeset 33 2d9b15f99b4e
parent 31 2c7fc855eba8
permissions -rw-r--r--
Front IDILL : search by curves added search by type added notifications added timeline improved
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
31
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
     1
/*
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
     2
  mustache.js — Logic-less templates in JavaScript
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
     3
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
     4
  See http://mustache.github.com/ for more info.
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
     5
*/
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
     6
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
     7
var Mustache = function () {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
     8
  var _toString = Object.prototype.toString;
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
     9
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    10
  Array.isArray = Array.isArray || function (obj) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    11
    return _toString.call(obj) == "[object Array]";
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    12
  }
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    13
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    14
  var _trim = String.prototype.trim, trim;
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    15
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    16
  if (_trim) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    17
    trim = function (text) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    18
      return text == null ? "" : _trim.call(text);
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    19
    }
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    20
  } else {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    21
    var trimLeft, trimRight;
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    22
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    23
    // IE doesn't match non-breaking spaces with \s.
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    24
    if ((/\S/).test("\xA0")) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    25
      trimLeft = /^[\s\xA0]+/;
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    26
      trimRight = /[\s\xA0]+$/;
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    27
    } else {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    28
      trimLeft = /^\s+/;
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    29
      trimRight = /\s+$/;
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    30
    }
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    31
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    32
    trim = function (text) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    33
      return text == null ? "" :
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    34
        text.toString().replace(trimLeft, "").replace(trimRight, "");
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    35
    }
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    36
  }
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    37
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    38
  var escapeMap = {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    39
    "&": "&",
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    40
    "<": "&lt;",
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    41
    ">": "&gt;",
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    42
    '"': '&quot;',
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    43
    "'": '&#39;'
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    44
  };
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    45
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    46
  function escapeHTML(string) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    47
    return String(string).replace(/&(?!\w+;)|[<>"']/g, function (s) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    48
      return escapeMap[s] || s;
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    49
    });
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    50
  }
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    51
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    52
  var regexCache = {};
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    53
  var Renderer = function () {};
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    54
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    55
  Renderer.prototype = {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    56
    otag: "{{",
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    57
    ctag: "}}",
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    58
    pragmas: {},
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    59
    buffer: [],
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    60
    pragmas_implemented: {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    61
      "IMPLICIT-ITERATOR": true
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    62
    },
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    63
    context: {},
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    64
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    65
    render: function (template, context, partials, in_recursion) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    66
      // reset buffer & set context
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    67
      if (!in_recursion) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    68
        this.context = context;
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    69
        this.buffer = []; // TODO: make this non-lazy
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    70
      }
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    71
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    72
      // fail fast
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    73
      if (!this.includes("", template)) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    74
        if (in_recursion) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    75
          return template;
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    76
        } else {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    77
          this.send(template);
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    78
          return;
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    79
        }
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    80
      }
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    81
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    82
      // get the pragmas together
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    83
      template = this.render_pragmas(template);
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    84
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    85
      // render the template
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    86
      var html = this.render_section(template, context, partials);
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    87
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    88
      // render_section did not find any sections, we still need to render the tags
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    89
      if (html === false) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    90
        html = this.render_tags(template, context, partials, in_recursion);
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    91
      }
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    92
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    93
      if (in_recursion) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    94
        return html;
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    95
      } else {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    96
        this.sendLines(html);
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    97
      }
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    98
    },
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    99
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   100
    /*
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   101
      Sends parsed lines
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   102
    */
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   103
    send: function (line) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   104
      if (line !== "") {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   105
        this.buffer.push(line);
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   106
      }
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   107
    },
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   108
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   109
    sendLines: function (text) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   110
      if (text) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   111
        var lines = text.split("\n");
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   112
        for (var i = 0; i < lines.length; i++) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   113
          this.send(lines[i]);
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   114
        }
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   115
      }
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   116
    },
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   117
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   118
    /*
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   119
      Looks for %PRAGMAS
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   120
    */
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   121
    render_pragmas: function (template) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   122
      // no pragmas
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   123
      if (!this.includes("%", template)) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   124
        return template;
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   125
      }
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   126
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   127
      var that = this;
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   128
      var regex = this.getCachedRegex("render_pragmas", function (otag, ctag) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   129
        return new RegExp(otag + "%([\\w-]+) ?([\\w]+=[\\w]+)?" + ctag, "g");
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   130
      });
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   131
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   132
      return template.replace(regex, function (match, pragma, options) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   133
        if (!that.pragmas_implemented[pragma]) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   134
          throw({message:
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   135
            "This implementation of mustache doesn't understand the '" +
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   136
            pragma + "' pragma"});
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   137
        }
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   138
        that.pragmas[pragma] = {};
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   139
        if (options) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   140
          var opts = options.split("=");
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   141
          that.pragmas[pragma][opts[0]] = opts[1];
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   142
        }
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   143
        return "";
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   144
        // ignore unknown pragmas silently
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   145
      });
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   146
    },
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   147
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   148
    /*
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   149
      Tries to find a partial in the curent scope and render it
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   150
    */
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   151
    render_partial: function (name, context, partials) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   152
      name = trim(name);
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   153
      if (!partials || partials[name] === undefined) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   154
        throw({message: "unknown_partial '" + name + "'"});
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   155
      }
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   156
      if (!context || typeof context[name] != "object") {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   157
        return this.render(partials[name], context, partials, true);
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   158
      }
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   159
      return this.render(partials[name], context[name], partials, true);
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   160
    },
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   161
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   162
    /*
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   163
      Renders inverted (^) and normal (#) sections
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   164
    */
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   165
    render_section: function (template, context, partials) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   166
      if (!this.includes("#", template) && !this.includes("^", template)) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   167
        // did not render anything, there were no sections
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   168
        return false;
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   169
      }
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   170
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   171
      var that = this;
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   172
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   173
      var regex = this.getCachedRegex("render_section", function (otag, ctag) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   174
        // This regex matches _the first_ section ({{#foo}}{{/foo}}), and captures the remainder
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   175
        return new RegExp(
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   176
          "^([\\s\\S]*?)" +         // all the crap at the beginning that is not {{*}} ($1)
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   177
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   178
          otag +                    // {{
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   179
          "(\\^|\\#)\\s*(.+)\\s*" + //  #foo (# == $2, foo == $3)
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   180
          ctag +                    // }}
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   181
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   182
          "\n*([\\s\\S]*?)" +       // between the tag ($2). leading newlines are dropped
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   183
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   184
          otag +                    // {{
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   185
          "\\/\\s*\\3\\s*" +        //  /foo (backreference to the opening tag).
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   186
          ctag +                    // }}
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   187
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   188
          "\\s*([\\s\\S]*)$",       // everything else in the string ($4). leading whitespace is dropped.
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   189
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   190
        "g");
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   191
      });
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   192
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   193
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   194
      // for each {{#foo}}{{/foo}} section do...
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   195
      return template.replace(regex, function (match, before, type, name, content, after) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   196
        // before contains only tags, no sections
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   197
        var renderedBefore = before ? that.render_tags(before, context, partials, true) : "",
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   198
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   199
        // after may contain both sections and tags, so use full rendering function
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   200
            renderedAfter = after ? that.render(after, context, partials, true) : "",
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   201
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   202
        // will be computed below
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   203
            renderedContent,
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   204
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   205
            value = that.find(name, context);
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   206
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   207
        if (type === "^") { // inverted section
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   208
          if (!value || Array.isArray(value) && value.length === 0) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   209
            // false or empty list, render it
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   210
            renderedContent = that.render(content, context, partials, true);
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   211
          } else {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   212
            renderedContent = "";
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   213
          }
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   214
        } else if (type === "#") { // normal section
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   215
          if (Array.isArray(value)) { // Enumerable, Let's loop!
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   216
            renderedContent = that.map(value, function (row) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   217
              return that.render(content, that.create_context(row), partials, true);
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   218
            }).join("");
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   219
          } else if (that.is_object(value)) { // Object, Use it as subcontext!
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   220
            renderedContent = that.render(content, that.create_context(value),
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   221
              partials, true);
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   222
          } else if (typeof value == "function") {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   223
            // higher order section
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   224
            renderedContent = value.call(context, content, function (text) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   225
              return that.render(text, context, partials, true);
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   226
            });
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   227
          } else if (value) { // boolean section
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   228
            renderedContent = that.render(content, context, partials, true);
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   229
          } else {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   230
            renderedContent = "";
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   231
          }
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   232
        }
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   233
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   234
        return renderedBefore + renderedContent + renderedAfter;
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   235
      });
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   236
    },
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   237
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   238
    /*
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   239
      Replace {{foo}} and friends with values from our view
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   240
    */
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   241
    render_tags: function (template, context, partials, in_recursion) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   242
      // tit for tat
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   243
      var that = this;
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   244
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   245
      var new_regex = function () {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   246
        return that.getCachedRegex("render_tags", function (otag, ctag) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   247
          return new RegExp(otag + "(=|!|>|&|\\{|%)?([^#\\^]+?)\\1?" + ctag + "+", "g");
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   248
        });
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   249
      };
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   250
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   251
      var regex = new_regex();
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   252
      var tag_replace_callback = function (match, operator, name) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   253
        switch(operator) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   254
        case "!": // ignore comments
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   255
          return "";
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   256
        case "=": // set new delimiters, rebuild the replace regexp
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   257
          that.set_delimiters(name);
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   258
          regex = new_regex();
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   259
          return "";
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   260
        case ">": // render partial
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   261
          return that.render_partial(name, context, partials);
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   262
        case "{": // the triple mustache is unescaped
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   263
        case "&": // & operator is an alternative unescape method
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   264
          return that.find(name, context);
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   265
        default: // escape the value
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   266
          return escapeHTML(that.find(name, context));
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   267
        }
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   268
      };
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   269
      var lines = template.split("\n");
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   270
      for(var i = 0; i < lines.length; i++) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   271
        lines[i] = lines[i].replace(regex, tag_replace_callback, this);
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   272
        if (!in_recursion) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   273
          this.send(lines[i]);
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   274
        }
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   275
      }
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   276
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   277
      if (in_recursion) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   278
        return lines.join("\n");
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   279
      }
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   280
    },
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   281
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   282
    set_delimiters: function (delimiters) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   283
      var dels = delimiters.split(" ");
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   284
      this.otag = this.escape_regex(dels[0]);
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   285
      this.ctag = this.escape_regex(dels[1]);
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   286
    },
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   287
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   288
    escape_regex: function (text) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   289
      // thank you Simon Willison
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   290
      if (!arguments.callee.sRE) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   291
        var specials = [
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   292
          '/', '.', '*', '+', '?', '|',
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   293
          '(', ')', '[', ']', '{', '}', '\\'
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   294
        ];
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   295
        arguments.callee.sRE = new RegExp(
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   296
          '(\\' + specials.join('|\\') + ')', 'g'
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   297
        );
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   298
      }
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   299
      return text.replace(arguments.callee.sRE, '\\$1');
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   300
    },
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   301
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   302
    /*
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   303
      find `name` in current `context`. That is find me a value
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   304
      from the view object
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   305
    */
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   306
    find: function (name, context) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   307
      name = trim(name);
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   308
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   309
      // Checks whether a value is thruthy or false or 0
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   310
      function is_kinda_truthy(bool) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   311
        return bool === false || bool === 0 || bool;
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   312
      }
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   313
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   314
      var value;
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   315
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   316
      // check for dot notation eg. foo.bar
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   317
      if (name.match(/([a-z_]+)\./ig)) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   318
        var childValue = this.walk_context(name, context);
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   319
        if (is_kinda_truthy(childValue)) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   320
          value = childValue;
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   321
        }
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   322
      } else {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   323
        if (is_kinda_truthy(context[name])) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   324
          value = context[name];
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   325
        } else if (is_kinda_truthy(this.context[name])) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   326
          value = this.context[name];
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   327
        }
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   328
      }
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   329
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   330
      if (typeof value == "function") {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   331
        return value.apply(context);
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   332
      }
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   333
      if (value !== undefined) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   334
        return value;
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   335
      }
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   336
      // silently ignore unkown variables
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   337
      return "";
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   338
    },
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   339
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   340
    walk_context: function (name, context) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   341
      var path = name.split('.');
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   342
      // if the var doesn't exist in current context, check the top level context
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   343
      var value_context = (context[path[0]] != undefined) ? context : this.context;
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   344
      var value = value_context[path.shift()];
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   345
      while (value != undefined && path.length > 0) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   346
        value_context = value;
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   347
        value = value[path.shift()];
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   348
      }
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   349
      // if the value is a function, call it, binding the correct context
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   350
      if (typeof value == "function") {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   351
        return value.apply(value_context);
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   352
      }
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   353
      return value;
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   354
    },
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   355
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   356
    // Utility methods
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   357
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   358
    /* includes tag */
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   359
    includes: function (needle, haystack) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   360
      return haystack.indexOf(this.otag + needle) != -1;
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   361
    },
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   362
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   363
    // by @langalex, support for arrays of strings
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   364
    create_context: function (_context) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   365
      if (this.is_object(_context)) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   366
        return _context;
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   367
      } else {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   368
        var iterator = ".";
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   369
        if (this.pragmas["IMPLICIT-ITERATOR"]) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   370
          iterator = this.pragmas["IMPLICIT-ITERATOR"].iterator;
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   371
        }
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   372
        var ctx = {};
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   373
        ctx[iterator] = _context;
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   374
        return ctx;
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   375
      }
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   376
    },
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   377
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   378
    is_object: function (a) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   379
      return a && typeof a == "object";
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   380
    },
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   381
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   382
    /*
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   383
      Why, why, why? Because IE. Cry, cry cry.
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   384
    */
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   385
    map: function (array, fn) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   386
      if (typeof array.map == "function") {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   387
        return array.map(fn);
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   388
      } else {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   389
        var r = [];
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   390
        var l = array.length;
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   391
        for(var i = 0; i < l; i++) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   392
          r.push(fn(array[i]));
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   393
        }
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   394
        return r;
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   395
      }
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   396
    },
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   397
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   398
    getCachedRegex: function (name, generator) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   399
      var byOtag = regexCache[this.otag];
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   400
      if (!byOtag) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   401
        byOtag = regexCache[this.otag] = {};
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   402
      }
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   403
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   404
      var byCtag = byOtag[this.ctag];
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   405
      if (!byCtag) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   406
        byCtag = byOtag[this.ctag] = {};
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   407
      }
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   408
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   409
      var regex = byCtag[name];
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   410
      if (!regex) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   411
        regex = byCtag[name] = generator(this.otag, this.ctag);
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   412
      }
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   413
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   414
      return regex;
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   415
    }
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   416
  };
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   417
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   418
  return({
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   419
    name: "mustache.js",
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   420
    version: "0.5.0-dev",
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   421
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   422
    /*
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   423
      Turns a template and view into HTML
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   424
    */
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   425
    to_html: function (template, view, partials, send_fun) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   426
      var renderer = new Renderer();
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   427
      if (send_fun) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   428
        renderer.send = send_fun;
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   429
      }
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   430
      renderer.render(template, view || {}, partials);
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   431
      if (!send_fun) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   432
        return renderer.buffer.join("\n");
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   433
      }
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   434
    }
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   435
  });
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   436
}();