123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250 |
-
- KISSY.add('template/base', function(S) {
- var
- templateCache = {},
-
- tagStartEnd = {
- '#': 'start',
- '/': 'end'
- },
-
- KS_TEMPL_STAT_PARAM = 'KS_TEMPL_STAT_PARAM',
- KS_TEMPL_STAT_PARAM_REG = new RegExp(KS_TEMPL_STAT_PARAM, "g"),
- KS_TEMPL = 'KS_TEMPL',
- KS_DATA = 'KS_DATA_',
- KS_AS = 'as',
-
- PREFIX = '");',
- SUFFIX = KS_TEMPL + '.push("',
- PARSER_SYNTAX_ERROR = 'KISSY.Template: Syntax Error. ',
- PARSER_RENDER_ERROR = 'KISSY.Template: Render Error. ',
- PARSER_PREFIX = 'var ' + KS_TEMPL + '=[],' +
- KS_TEMPL_STAT_PARAM + '=false;with(',
- PARSER_MIDDLE = '||{}){try{' + KS_TEMPL + '.push("',
- PARSER_SUFFIX = '");}catch(e){' + KS_TEMPL + '=["' +
- PARSER_RENDER_ERROR + '" + e.message]}};return ' +
- KS_TEMPL + '.join("");',
-
- restoreQuote = function(str) {
- return str.replace(/\\"/g, '"');
- },
-
- escapeQuote = function(str) {
- return str.replace(/"/g, '\\"');
- },
- trim = S.trim,
-
- buildParser = function(tpl) {
- var _parser,
- _empty_index;
- return escapeQuote(trim(tpl)
- .replace(/[\r\t\n]/g, ' ')
-
-
- .replace(/\\/g, '\\\\'))
- .replace(/\{\{([#/]?)(?!\}\})([^}]*)\}\}/g,
- function(all, expr, body) {
- _parser = "";
-
- body = restoreQuote(trim(body));
-
-
- if (expr) {
- _empty_index = body.indexOf(' ');
- body = _empty_index === -1 ?
- [ body, '' ] :
- [
- body.substring(0, _empty_index),
- body.substring(_empty_index)
- ];
- var operator = body[0],
- fn,
- args = trim(body[1]),
- opStatement = Statements[operator];
- if (opStatement && tagStartEnd[expr]) {
-
- fn = opStatement[tagStartEnd[expr]];
- _parser = String(S.isFunction(fn) ?
- fn.apply(this, args.split(/\s+/)) :
- fn.replace(KS_TEMPL_STAT_PARAM_REG, args));
- }
- }
-
- else {
- _parser = KS_TEMPL +
- '.push(' +
-
-
- 'typeof (' + body + ') ==="undefined"?"":' + body +
- ');';
- }
- return PREFIX + _parser + SUFFIX;
- });
- },
-
- Statements = {
- 'if': {
- start: 'if(typeof (' + KS_TEMPL_STAT_PARAM + ') !=="undefined" && ' + KS_TEMPL_STAT_PARAM + '){',
- end: '}'
- },
- 'else': {
- start: '}else{'
- },
- 'elseif': {
- start: '}else if(' + KS_TEMPL_STAT_PARAM + '){'
- },
-
- 'each': {
- start: function(obj, as, v, k) {
- var _ks_value = '_ks_value',
- _ks_index = '_ks_index';
- if (as === KS_AS && v) {
- _ks_value = v || _ks_value,
- _ks_index = k || _ks_index;
- }
- return 'KISSY.each(' + obj +
- ', function(' + _ks_value +
- ', ' + _ks_index + '){';
- },
- end: '});'
- },
-
- '!': {
- start: '/*' + KS_TEMPL_STAT_PARAM + '*/'
- }
- };
-
- function Template(tpl) {
- if (!(templateCache[tpl])) {
- var _ks_data = S.guid(KS_DATA),
- func,
- o,
- _parser = [
- PARSER_PREFIX,
- _ks_data,
- PARSER_MIDDLE,
- o = buildParser(tpl),
- PARSER_SUFFIX
- ];
- try {
- func = new Function(_ks_data, _parser.join(""));
- } catch (e) {
- _parser[3] = PREFIX + SUFFIX +
- PARSER_SYNTAX_ERROR + ',' +
- e.message + PREFIX + SUFFIX;
- func = new Function(_ks_data, _parser.join(""));
- }
- templateCache[tpl] = {
- name: _ks_data,
- o:o,
- parser: _parser.join(""),
- render: func
- };
- }
- return templateCache[tpl];
- }
- S.mix(Template, {
-
- log: function(tpl) {
- if (tpl in templateCache) {
- if ('js_beautify' in window) {
- } else {
- S.log(templateCache[tpl].parser, 'info');
- }
- } else {
- Template(tpl);
- this.log(tpl);
- }
- },
-
- addStatement: function(statement, o) {
- if (S.isString(statement)) {
- Statements[statement] = o;
- } else {
- S.mix(Statements, statement);
- }
- }
- });
- return Template;
- });
- KISSY.add('template/node', function(S, Template, Node) {
- var $ = Node.all;
- S.mix(S, {
- tmpl: function(selector, data) {
- return $(Template($(selector).html()).render(data));
- }
- });
- }, {requires:["./base",'node']});
- KISSY.add("template", function(S, T) {
- S.Template = T;
- return T;
- }, {
- requires:["template/base","template/node"]
- });
|