|
var Template = Class.create({
//初始化方法
initialize: function(template, pattern) {
this.template = template.toString();
this.pattern = pattern || Template.Pattern;
},
//格式化方法,如果從Java的角度來(lái)說(shuō),其實(shí)叫format更好 :)
evaluate: function(object) {
//檢查是否定義了toTemplateReplacements方法,是的話調(diào)用
//整個(gè)的Prototype框架中,只有Hash對(duì)象定義了這個(gè)方法
if (object && Object.isFunction(object.toTemplateReplacements))
object = object.toTemplateReplacements();
//這里的gsub是String對(duì)象里面的方法,可以簡(jiǎn)單的認(rèn)為就是替換字符串中所有匹配pattern的部分
return this.template.gsub(this.pattern, function(match) {
//match[0]是整個(gè)的匹配Template.Pattern的字符串
//match[1]是匹配字符串前面的一個(gè)字符
//match[2]是匹配${var}這個(gè)表達(dá)式的部分
//match[3]是'#{var}'表達(dá)式的'var'部分
//如果object為null,則把所有的${var}表達(dá)式替換成''
if (object == null) return (match[1] + '');
//取得匹配表達(dá)式前一個(gè)字符
var before = match[1] || '';
//如果前一個(gè)字符串為'/',則直接返回匹配的表達(dá)式,不進(jìn)行替換
if (before == '//') return match[2];
var ctx = object, expr = match[3];
//這個(gè)正則表達(dá)式好像就是檢查var是否是合法的名稱,暫時(shí)沒(méi)看懂這個(gè)正則表達(dá)式的意義?
var pattern = /^([^.[]+|/[((?:.*?[^//])?)/])(/.|/[|$)/;
match = pattern.exec(expr);
//如果var不符合要求,則直接返回前一個(gè)字符
if (match == null) return before;
//逐個(gè)替換'#{var}'表達(dá)式部分
while (match != null) {
//不懂下面這個(gè)檢查什么意思?
var comp = match[1].startsWith('[') ? match[2].gsub('////]', ']') : 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);
}
//返回替換后的結(jié)果,'#{var}' ==> 'value'
return before + String.interpret(ctx);
});
}
});
//默認(rèn)的模板匹配正則表達(dá)式,形如#{var},很像JSP中的EL表達(dá)式
Template.Pattern = /(^|.|/r|/n)(#/{(.*?)/})/;
上面基本上把evaluate方法講了一遍,有些地方?jīng)]怎么看明白,那些正則表達(dá)式太難懂了。。。誰(shuí)知道的告訴我?
下面看一下示例:
復(fù)制代碼 代碼如下:
var myTemplate = new Template('The TV show #{title} was created by #{author}.');
var show = {title: 'The Simpsons', author: 'Matt Groening', NETwork: 'FOX' };
myTemplate.evaluate(show);
// -> The TV show The Simpsons was created by Matt Groening.
復(fù)制代碼 代碼如下:
var t = new Template('in #{lang} we also use the //#{variable} syntax for templates.');
var data = {lang:'Ruby', variable: '(not used)'}; t.evaluate(data);
// -> in Ruby we also use the #{variable} syntax for templates.
復(fù)制代碼 代碼如下:
//自定義匹配模式
var syntax = /(^|.|/r|/n)(/<%=/s*(/w+)/s*%/>)/;
//matches symbols like '<%= field %>'
var t = new Template('<div>Name: <b><%= name %></b>, Age: <b><%=age%></b></div>', syntax);
t.evaluate( {name: 'John Smith', age: 26} );
// -> <div>Name: <b>John Smith</b>, Age: <b>26</b></div>
復(fù)制代碼 代碼如下:
var conversion1 = {from: 'meters', to: 'feet', factor: 3.28};
var conversion2 = {from: 'kilojoules', to: 'BTUs', factor: 0.9478};
var conversion3 = {from: 'megabytes', to: 'gigabytes', factor: 1024};
var templ = new Template('Multiply by #{factor} to convert from #{from} to #{to}.');
[conversion1, conversion2, conversion3].each( function(conv){ templ.evaluate(conv); });
// -> Multiply by 3.28 to convert from meters to feet.
// -> Multiply by 0.9478 to convert from kilojoules to BTUs.
// -> Multiply by 1024 to convert from megabytes to gigabytes.
JavaScript技術(shù):Prototype Template對(duì)象 學(xué)習(xí),轉(zhuǎn)載需保留來(lái)源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。