-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate.js
More file actions
76 lines (55 loc) · 2.57 KB
/
Copy pathtemplate.js
File metadata and controls
76 lines (55 loc) · 2.57 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
var ConText = require('@triskel/con-text').ConText,
parseTemplate = require('./parser'),
renderTokens = require('./render'),
preset_statements = require('./preset-statements')
function _compileTemplate(con_Text, statements, self_closed_statements, template_src) {
var tokens = parseTemplate(template_src, self_closed_statements)
return function (data) {
return renderTokens(con_Text, tokens, data, statements)
}
}
function createTemplateContext () {
var con_Text = new ConText()
var templates_cache = {}
var statements = Object.create(preset_statements)
var self_closed_statements = {}
function template (template_src, data) {
if( typeof template_src !== 'string' ) throw new TypeError('template source should be a String')
return data === undefined ?
_compileTemplate(con_Text, statements, self_closed_statements, template_src) :
renderTokens(con_Text, parseTemplate(template_src, self_closed_statements), data, statements)
}
template.context = createTemplateContext
function _throwType (text, received) {
throw new TypeError(text + ', received: ' + (typeof received) )
}
template.statement = function (statement_name, statementFn, self_closed) {
if( typeof statement_name !== 'string' ) return _throwType('statement (cmd) name should be a String', statement_name )
if( typeof statementFn !== 'function' ) return _throwType('statement function should be a Function', statementFn )
if( self_closed ) self_closed_statements[statement_name] = true
statements[statement_name] = statementFn
return template
}
template.cmd = template.statement
template.filter = function (filter_name, filterFn) {
if( typeof filter_name !== 'string' ) _throwType('filter name should be a String', filter_name )
if( typeof filterFn !== 'function' ) _throwType('filter function should be a Function', filterFn )
con_Text.defineFilter(filter_name, filterFn)
return template
}
template.compile = function (template_src) {
return _compileTemplate(con_Text, statements, self_closed_statements, template_src)
}
template.render = function (template_src, data) {
return renderTokens(con_Text, parseTemplate(template_src, self_closed_statements), data, statements)
}
template.put = function (template_name, template_src) {
templates_cache[template_name] = _compileTemplate(con_Text, statements, self_closed_statements, template_src)
return template
}
template.get = function (template_name) {
return templates_cache[template_name]
}
return template
}
module.exports = createTemplateContext()