All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.clickntap.square.javascript.template-lib.js Maven / Gradle / Ivy

var templateLib = (function () {
  var templates = {};
  var beginTag = '[';
  beginTag += '%';
  var endTag = '%';
  endTag += ']';
  function parseTemplate(html) {
    var code = '';
    code += '(function (model){\n';
      code += '  if(model)\n';
      code += '    for(var key in model)\n';
      code += '      eval("var "+key+"=model."+key);\n';
      code += '  var code = "";\n';
      var x0 = 0;
      var x1 = 0;
      try {
        while((x0 = html.indexOf(beginTag, x0)) >= 0) {
          x1 = html.indexOf(endTag);
          code += '  code += '+JSON.stringify(html.substring(0, x0))+';\n';
          var js = html.substring(x0+2, x1);
          if(js.charAt(0) == '=') {
            js = js.substring(1);
            code += '  code +=  '+js+';\n';
          } else {
            code += js+';\n';
          }
          html = html.substring(x1+2);
          x0 = x1 = 0;
        }
        code += '  code += '+JSON.stringify(html)+';\n';
      } catch(err) {
        console.error(err, html);
      }
    code += '  return code;\n})\n';
    return eval(code);
  }
  var templateLibrary = function (pathOrCode, code) {
    if(code) {
      this.template = templates[pathOrCode] = parseTemplate(code);
    } else {
      this.template = templates[pathOrCode];
      if(!this.template) {
        if(pathOrCode) {
          this.template = parseTemplate(pathOrCode);
        }
      }
    }
  };
  templateLibrary.prototype.render = function(json) {
    try {
      return this.template(json);
    } catch(err) {
      return err;
    }
  }
  var templateInstance = function (pathOrCode, code) {
    return new templateLibrary(pathOrCode, code);
  };
  return templateInstance;
})();




© 2015 - 2025 Weber Informatics LLC | Privacy Policy