dynamictemplate
Version:
Δt - async & dynamic templating engine
117 lines (93 loc) • 2.99 kB
JavaScript
(function() {
var DefaultBuilder, cache, clear, create, ff, get, lookup, pp,
__hasProp = {}.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
DefaultBuilder = require('asyncxml').Builder;
cache = {};
pp = function(proto, name) {
proto[name] = function() {
var _ref;
return this.tag.apply(this, (_ref = [name]).concat.apply(_ref, arguments));
};
proto["$" + name] = function() {
var _ref;
return this.$tag.apply(this, (_ref = [name]).concat.apply(_ref, arguments));
};
};
ff = function(proto, tags) {
var tagname, _i, _len;
for (_i = 0, _len = tags.length; _i < _len; _i++) {
tagname = tags[_i];
if (tagname) {
pp(proto, tagname);
}
}
};
get = function(key, opts) {
var ExtendedBuilder, xml;
ExtendedBuilder = cache[key].Builder;
xml = new ExtendedBuilder(opts);
xml.Tag = xml.opts.Tag = cache[key].Tag;
return xml;
};
create = function(key, opts) {
var Builder, ExtendedBuilder, ExtendedTag, Tag, xml, _ref, _ref1, _ref2;
Builder = (_ref = opts.Builder) != null ? _ref : DefaultBuilder;
ExtendedBuilder = (function(_super) {
__extends(ExtendedBuilder, _super);
function ExtendedBuilder() {
_ref1 = ExtendedBuilder.__super__.constructor.apply(this, arguments);
return _ref1;
}
return ExtendedBuilder;
})(Builder);
ff(ExtendedBuilder.prototype, opts.schema);
xml = new ExtendedBuilder(opts);
Tag = xml.Tag;
ExtendedTag = (function(_super) {
__extends(ExtendedTag, _super);
function ExtendedTag() {
_ref2 = ExtendedTag.__super__.constructor.apply(this, arguments);
return _ref2;
}
ExtendedTag.prototype.partial = function(partial) {
if (partial.run == null) {
return this;
}
if (partial.started) {
console.warn("partial already started!", "you can delay it by adding option {partial:true} to your template");
return this;
}
this.add(partial);
partial.run();
return this;
};
return ExtendedTag;
})(Tag);
ff(ExtendedTag.prototype, opts.schema);
xml.Tag = xml.opts.Tag = ExtendedTag;
cache[key] = {
Builder: ExtendedBuilder,
Tag: ExtendedTag
};
return xml;
};
lookup = function(opts) {
var key, _ref;
key = (_ref = opts._schema) != null ? _ref : opts.schema;
if (cache[key] != null) {
return get(key, opts);
} else {
return create(key, opts);
}
};
clear = function() {
return cache = {};
};
module.exports = {
create: create,
lookup: lookup,
clear: clear,
get: get
};
}).call(this);