Prototype Template對象 學習
更新時間:2009年07月19日 01:20:14 作者:
這里的Template對象其實就是格式化字符串的工具,就像java中的String.format方法。這個對象只提供一個方法evaluate。
復制代碼 代碼如下:
var Template = Class.create({
//初始化方法
initialize: function(template, pattern) {
this.template = template.toString();
this.pattern = pattern || Template.Pattern;
},
//格式化方法,如果從java的角度來說,其實叫format更好 :)
evaluate: function(object) {
//檢查是否定義了toTemplateReplacements方法,是的話調(diào)用
//整個的Prototype框架中,只有Hash對象定義了這個方法
if (object && Object.isFunction(object.toTemplateReplacements))
object = object.toTemplateReplacements();
//這里的gsub是String對象里面的方法,可以簡單的認為就是替換字符串中所有匹配pattern的部分
return this.template.gsub(this.pattern, function(match) {
//match[0]是整個的匹配Template.Pattern的字符串
//match[1]是匹配字符串前面的一個字符
//match[2]是匹配${var}這個表達式的部分
//match[3]是'#{var}'表達式的'var'部分
//如果object為null,則把所有的${var}表達式替換成''
if (object == null) return (match[1] + '');
//取得匹配表達式前一個字符
var before = match[1] || '';
//如果前一個字符串為'\',則直接返回匹配的表達式,不進行替換
if (before == '\\') return match[2];
var ctx = object, expr = match[3];
//這個正則表達式好像就是檢查var是否是合法的名稱,暫時沒看懂這個正則表達式的意義?
var pattern = /^([^.[]+|\[((?:.*?[^\\])?)\])(\.|\[|$)/;
match = pattern.exec(expr);
//如果var不符合要求,則直接返回前一個字符
if (match == null) return before;
//逐個替換'#{var}'表達式部分
while (match != null) {
//不懂下面這個檢查什么意思?
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);
});
}
});
//默認的模板匹配正則表達式,形如#{var},很像JSP中的EL表達式
Template.Pattern = /(^|.|\r|\n)(#\{(.*?)\})/;
上面基本上把evaluate方法講了一遍,有些地方?jīng)]怎么看明白,那些正則表達式太難懂了。。。誰知道的告訴我?
下面看一下示例:
復制代碼 代碼如下:
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.
復制代碼 代碼如下:
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.
復制代碼 代碼如下:
//自定義匹配模式
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>
復制代碼 代碼如下:
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.
相關(guān)文章
prototype Element學習筆記(Element篇三)
上一篇把Element的所函數(shù)都梳理了一遍,下面總結(jié)一下這些函數(shù)的功能,畢竟函數(shù)太多,不分門別類一下還是沒有底。2008-10-10prototype 1.5 & scriptaculous 1.6.1 學習筆記
prototype 1.5 & scriptaculous 1.6.1 學習筆記...2006-09-09初學prototype,發(fā)個JS接受URL參數(shù)的代碼
初學prototype,發(fā)個JS接受URL參數(shù)的代碼...2007-01-01