Hi,

I've been working on a Template rewrite trying to reduce code size and  
complexity.
It uses `String#replace` instead of `String#gsub` and thus performs  
better.
Do you guys see any enhancements (in terms of performance or code  
size) that could be made ?

Diff: 
http://github.com/samleb/prototype/commit/8ba0a0b80c5ad230cec32e766360e0e6be7e956c#diff-0
File: 
http://github.com/samleb/prototype/blob/8ba0a0b80c5ad230cec32e766360e0e6be7e956c/src/lang/template.js

Best,
Samuel.

On 12 août 09, at 20:37, Allen wrote:

>
> Sorry, misunderstood what you were asking. I thought you meant why
> can't the values be populated into the string at initialization.
>
> On Aug 12, 2:32 pm, Yaffle <vic99...@yandex.ru> wrote:
>> String#interpolate uses Template#evaluate.
>> This Template realization makes some actions in constructor of
>> Template class instead of Template#evaluate.
>> So if you will use Template#evaluate many times for one Template
>> object, you will save a little time.
>>
>> On Aug 12, 6:41 pm, Allen <bla...@gmail.com> wrote:
>>
>>
>>
>>> You should use Strings interpolate function for this. A template is
>>> meant to be reused many times with many different filler values.
>>
>>> On Jun 14, 10:41 am, Yaffle <vic99...@yandex.ru> wrote:
>>
>>>> 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