var Template = Class.create({
  initialize: function(template, pattern){
    var parts = template.toString().split(pattern ||
Template.Pattern),
        pl = parts.length,
        pattern2 = /^([^.\[]+|\[((?:.*?[^\\])?)\])(\.|\[|$)/;

    for(var i=1,k=1;i<pl;i+=4){
      var before = parts[i] || '',
         escaped = parts[i+1];
      if(before=='\\'){
        parts[k-1] += escaped;
      }else{
        parts[k-1] += before;

        var propList = [],
            expr  = parts[i+2],
            match = pattern2.exec(expr);
        while(match){
          propList.push( match[1].startsWith('[') ? match[2].replace(/\
\\\\]/g, ']') : match[1] );
          if(!match[3]){
            break;
          }
          expr = expr.substring('[' == match[3] ? match[1].length :
match[0].length);
          match = pattern2.exec(expr);
        }

        if(propList.length){
          parts[k]   = propList;
          parts[k+1] = parts[i+3];
          k+=2;
        }else{
          parts[k-1] += parts[i+3];
        }

      }
    }
    parts.length = k;
    this.parts = parts;
  },
  evaluate: function(object){
    if(Object.isFunction(object.toTemplateReplacements)){
      object = object.toTemplateReplacements();
    }
    object = object || '';

    var r = this.parts[0];
    for(var i=1,pl=this.parts.length;i<pl;i+=2){
      var propList = this.parts[i],
          ctx = object;
      for(var j=0,l = propList.length;j<l && ctx;j++){
        ctx = ctx[propList[j]];
      }
      r+= String.interpret(ctx)+this.parts[i+1];
    }
    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