prototype/src/lang/template.js

52 lines
1.6 KiB
JavaScript
Raw Normal View History

2008-12-14 04:36:59 +00:00
/** section: lang
* class Template
**/
var Template = Class.create({
2008-12-14 04:36:59 +00:00
/**
* new Template(template[, pattern = Template.Pattern])
*
2009-02-24 02:35:49 +00:00
* Creates a Template object.
*
* The optional `pattern` argument expects a `RegExp` that defines a custom
* syntax for the replaceable symbols in `template`.
2008-12-14 04:36:59 +00:00
**/
initialize: function(template, pattern) {
this.template = template.toString();
this.pattern = pattern || Template.Pattern;
},
2008-12-14 04:36:59 +00:00
/**
* Template#evaluate(object) -> String
*
* Applies the template to given `object`s data, producing a formatted string
* with symbols replaced by corresponding objects properties.
**/
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)(#\{(.*?)\})/;