var Template = Class.create({
  initialize: function(template, pattern) {
    this.template = template.toString();
    pattern = pattern || Template.Pattern;
    if (!Object.isString(pattern) && pattern.global)
      pattern = new RegExp(pattern.source);
    this.pattern = pattern;

    var parts = this.template.split(this.pattern);
    var rfunc = function(object, before, escaped, expr) {
      if (object == null) return '';

      if (before == '\\') return escaped;
      if (before == null) before = '';

      var ctx = object;
      var pattern = /^([^.[]+|\[((?:.*?[^\\])?)\])(\.|\[|$)/;
      var match = pattern.exec(expr);
      if (match == null) return before;

      while (match != null) {
        var comp = match[1].startsWith('[') ? match[2].replace(/\\\\]/
g, ']') : match[1];
        ctx = ctx[comp];
        if (null == ctx || '' == match[3]) break;
        expr = expr.substring('[' == match[3] ? match[1].length : match
[0].length);
        match = pattern.exec(expr);
      }

      return before + String.interpret(ctx);
    };
    this.evaluate = function(object){
      if (Object.isFunction(object.toTemplateReplacements)){
        object = object.toTemplateReplacements();
      }

      var r = parts[0], l = parts.length;
      for(var i=1;i<l;i+=4){
        r+= rfunc(object,parts[i],parts[i+1],parts[i+2])+parts[i+3];
      }
      return r;
    };
  }
});
Template.Pattern = /(^|.|\r|\n)(#\{(.*?)\})/;

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Prototype: Core" group.
To post to this group, send email to prototype-core@googlegroups.com
To unsubscribe from this group, send email to 
prototype-core-unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/prototype-core?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to