|
| 1 | +/** |
| 2 | + * Underscore.template - More APIs for Underscore's template engine. |
| 3 | + * Released under the MIT license. |
| 4 | + * https://github.com/cssmagic/underscore.template |
| 5 | + */ |
| 6 | +var template = function () { |
| 7 | + 'use strict' |
| 8 | + |
| 9 | + //namespace |
| 10 | + var template = {} |
| 11 | + |
| 12 | + //config |
| 13 | + var ELEM_ID_PREFIX = 'template-' |
| 14 | + |
| 15 | + //cache |
| 16 | + var _cacheTemplate = {} |
| 17 | + var _cacheCompiledTemplate = {} |
| 18 | + |
| 19 | + //util |
| 20 | + function _toTemplateId(id) { |
| 21 | + //`#template-my-tpl-001` -> `my-tpl-001` |
| 22 | + // `template-my-tpl-001` -> `my-tpl-001` |
| 23 | + // `my-tpl-001` -> `my-tpl-001` |
| 24 | + id = id ? _.str.trim(id).replace(/^[#!]+/, '') : '' |
| 25 | + return _.str.trim(id).replace(ELEM_ID_PREFIX, '') |
| 26 | + } |
| 27 | + function _toElementId(id) { |
| 28 | + //`template-my-tpl-001` -> `template-my-tpl-001` |
| 29 | + // `my-tpl-001` -> `template-my-tpl-001` |
| 30 | + id = id ? _.str.trim(id) : '' |
| 31 | + return _.str.startsWith(id, ELEM_ID_PREFIX) ? id : ELEM_ID_PREFIX + id |
| 32 | + } |
| 33 | + function _stripCommentTag(str) { |
| 34 | + str = String(str) |
| 35 | + if (_.str.startsWith(str, '<!' + '--') && _.str.endsWith(str, '-->')) { |
| 36 | + str = str.replace(/^<!\-\-/, '').replace(/\-\->$/, '') |
| 37 | + str = _.str.trim(str) |
| 38 | + } |
| 39 | + return str |
| 40 | + } |
| 41 | + //get template by id (of dummy script element in html) |
| 42 | + function _getTemplateById(id) { |
| 43 | + if (!id) return false |
| 44 | + var result |
| 45 | + var elementId = _toElementId(String(id)) |
| 46 | + var elem = document.getElementById(elementId) |
| 47 | + if (elem) { |
| 48 | + var str = _.str.trim(elem.innerHTML) |
| 49 | + if (str) { |
| 50 | + //strip html comment tag wrapping template code |
| 51 | + //especially for jedi 1.0 (https://github.com/baixing/jedi) |
| 52 | + if (_.templateSettings.shouldUnwrapCommentTag) str = _stripCommentTag(str) |
| 53 | + |
| 54 | + if (_isTemplateCode(str)) { |
| 55 | + result = str |
| 56 | + } else { |
| 57 | + /** DEBUG_INFO_START **/ |
| 58 | + console.warn('[Template] Template code in element "#' + elementId + '" is invalid!') |
| 59 | + /** DEBUG_INFO_END **/ |
| 60 | + } |
| 61 | + } else { |
| 62 | + /** DEBUG_INFO_START **/ |
| 63 | + console.warn('[Template] Element "#' + elementId + '" is empty!') |
| 64 | + /** DEBUG_INFO_END **/ |
| 65 | + } |
| 66 | + } else { |
| 67 | + /** DEBUG_INFO_START **/ |
| 68 | + console.warn('[Template] Element "#' + elementId + '" not found!') |
| 69 | + /** DEBUG_INFO_END **/ |
| 70 | + } |
| 71 | + return result || false |
| 72 | + } |
| 73 | + function _isTemplateCode(s) { |
| 74 | + var code = String(s) |
| 75 | + return _.str.include(code, '<%') && _.str.include(code, '%>') && /\bdata\b/.test(code) |
| 76 | + } |
| 77 | + |
| 78 | + //fn |
| 79 | + function add(id, templateCode) { |
| 80 | + //todo: accept second param as a function, to support pre-compiled template. |
| 81 | + if (arguments.length < 2) return false |
| 82 | + |
| 83 | + var result |
| 84 | + if (templateCode) { |
| 85 | + var templateId = _toTemplateId(id) |
| 86 | + /** DEBUG_INFO_START **/ |
| 87 | + if (_cacheTemplate[templateId]) { |
| 88 | + console.warn('[Template] Template id "' + templateId + '" already existed.') |
| 89 | + } |
| 90 | + /** DEBUG_INFO_END **/ |
| 91 | + result = _cacheTemplate[templateId] = templateCode |
| 92 | + } else { |
| 93 | + //todo: support `_.template.add(id)` to add from dummy script element |
| 94 | + //console.error('Missing template code to add to cache.') |
| 95 | + } |
| 96 | + return !!result |
| 97 | + } |
| 98 | + |
| 99 | + //api |
| 100 | + template.remove = function (/* id */) { |
| 101 | + //todo: remove template from cache (both str and fn) |
| 102 | + //todo: remove dummy script element |
| 103 | + } |
| 104 | + template.add = add |
| 105 | + template.render = function (id, data) { |
| 106 | + //todo: support _.template.render(templateCode, templateData) |
| 107 | + if (arguments.length < 2) { |
| 108 | + console.error('Missing data to render template: "' + id + '"') |
| 109 | + return false |
| 110 | + } |
| 111 | + var result |
| 112 | + var templateId = _toTemplateId(id) |
| 113 | + |
| 114 | + //todo: refactor: use recursion to simplify these codes |
| 115 | + //search in _cacheCompiledTemplate |
| 116 | + var fn = _cacheCompiledTemplate[templateId] |
| 117 | + var templateCode = _cacheTemplate[templateId] |
| 118 | + if (_.isFunction(fn)) { |
| 119 | + result = fn(data) |
| 120 | + } |
| 121 | + //search in _cacheTemplate |
| 122 | + else if (_.isString(templateCode)) { |
| 123 | + fn = _.template(templateCode) |
| 124 | + _cacheCompiledTemplate[templateId] = fn |
| 125 | + result = fn(data) |
| 126 | + } |
| 127 | + //get template code from dom |
| 128 | + else { |
| 129 | + templateCode = _getTemplateById(templateId) |
| 130 | + if (templateCode) { |
| 131 | + _cacheTemplate[templateId] = templateCode |
| 132 | + fn = _.template(templateCode) |
| 133 | + _cacheCompiledTemplate[templateId] = fn |
| 134 | + result = fn(data) |
| 135 | + } |
| 136 | + } |
| 137 | + return result || '' |
| 138 | + } |
| 139 | + |
| 140 | + /** DEBUG_INFO_START **/ |
| 141 | + //exports for unit test |
| 142 | + template.__toTemplateId = _toTemplateId |
| 143 | + template.__toElementId = _toElementId |
| 144 | + template.__isTemplateCode = _isTemplateCode |
| 145 | + template.__stripCommentTag = _stripCommentTag |
| 146 | + template.__cacheTemplate = _cacheTemplate |
| 147 | + template.__cacheCompiledTemplate = _cacheCompiledTemplate |
| 148 | + /** DEBUG_INFO_END **/ |
| 149 | + |
| 150 | + //exports |
| 151 | + return template |
| 152 | + |
| 153 | +}() |
0 commit comments