35 lines
1.1 KiB
JavaScript
35 lines
1.1 KiB
JavaScript
|
var Template = Class.create({
|
||
|
initialize: function(template, pattern) {
|
||
|
this.template = template.toString();
|
||
|
this.pattern = pattern || Template.Pattern;
|
||
|
},
|
||
|
|
||
|
evaluate: function(object) {
|
||
|
if (Object.isFunction(object.toTemplateReplacements))
|
||
|
object = object.toTemplateReplacements();
|
||
|
|
||
|
return this.template.gsub(this.pattern, function(match) {
|
||
|
if (object == null) return '';
|
||
|
|
||
|
var before = match[1] || '';
|
||
|
if (before == '\\') return match[2];
|
||
|
|
||
|
var ctx = object, expr = match[3];
|
||
|
var pattern = /^([^.[]+|\[((?:.*?[^\\])?)\])(\.|\[|$)/;
|
||
|
match = pattern.exec(expr);
|
||
|
if (match == null) return before;
|
||
|
|
||
|
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);
|
||
|
}
|
||
|
|
||
|
return before + String.interpret(ctx);
|
||
|
});
|
||
|
}
|
||
|
});
|
||
|
Template.Pattern = /(^|.|\r|\n)(#\{(.*?)\})/;
|