-
Notifications
You must be signed in to change notification settings - Fork 26
/
jquery.tmpl.js
40 lines (35 loc) · 1.19 KB
/
jquery.tmpl.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/*
* jQuery Simple Templates plugin 1.1.1
*
* http:https://andrew.hedges.name/tmpl/
* http:https://docs.jquery.com/Plugins/Tmpl
*
* Copyright (c) 2008 Andrew Hedges, [email protected]
*
* Usage: $.tmpl('<div class="#{classname}">#{content}</div>', { 'classname' : 'my-class', 'content' : 'My content.' });
*
* The changes for version 1.1 were inspired by the discussion at this thread:
* http:https://groups.google.com/group/jquery-ui/browse_thread/thread/45d0f5873dad0178/0f3c684499d89ff4
*
* Dual licensed under the MIT and GPL licenses:
* http:https://www.opensource.org/licenses/mit-license.php
* http:https://www.gnu.org/licenses/gpl.html
*/
(function($) {
$.extend({
// public interface: $.tmpl
tmpl : function(tmpl, vals) {
var rgxp, repr;
// default to doing no harm
tmpl = tmpl || '';
vals = vals || {};
// regular expression for matching our placeholders; e.g., #{my-cLaSs_name77}
rgxp = /#\{([^{}]*)}/g;
// function to making replacements
repr = function (str, match) {
return typeof vals[match] === 'string' || typeof vals[match] === 'number' ? vals[match] : str;
};
return tmpl.replace(rgxp, repr);
}
});
})(jQuery);