-
Notifications
You must be signed in to change notification settings - Fork 3
/
jstemplatemanager.sublime-snippet
54 lines (43 loc) · 1.58 KB
/
jstemplatemanager.sublime-snippet
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<snippet>
<content><![CDATA[
wds.util = wds.util || {};
wds.util.templatemanager = function() {
if ( wds.util.templatemanager.prototype._singletoninstance ) {
return wds.util.templatemanager.prototype._singletoninstance;
}
// "private" variables
var templates = {};
var tpl_regex = new regexp();
wds.util.templatemanager.prototype._singletoninstance = this;
this.render = function ( id, variables ) {
if ( ! this.get_tpl( id ) ) {
throw "could not find template " + id;
}
return apply_data( this.get_tpl( id ), variables );
};
this.add_tpl = function( id, template ) {
templates[ id ] = template;
};
this.get_tpl = function( id ) {
return templates[ id ] || false;
};
function apply_data( template, variables ) {
for ( var key in variables ) {
if ( ! variables.hasownproperty( key ) ) {
continue;
}
tpl_regex.compile( '{' + key + '}', 'g' );
template = template.replace( tpl_regex, variables[ key ] );
}
return template;
}
};
// Now, no matter how many times we contruct this object...
var MyTemplateUtility = new WDS.util.TemplateManager();
MyTemplateUtility.add_tpl( 'my_template', '<div id="{div_id}">{div_content}</div>' );
var SomeOtherTemplateUtility = new WDS.util.TemplateManager();
console.log( SomeOtherTemplateUtility.get_tpl( 'my_template' ) ); // <div id="{div_id}">{div_content}</div>
]]></content>
<tabTrigger>jstemplatemanager</tabTrigger>
<scope>source.js -string</scope>
</snippet>