Skip to content

Commit 6453092

Browse files
committed
[jsont] - Implement render_template built-in function
1 parent bc6541c commit 6453092

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

jsont/jsont.js

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,11 @@ function hash(method) {
8787
}
8888
}
8989

90-
fs.readFile(path_input, function(err, input_data) {
91-
input_data = JSON.parse(input_data)
92-
console.log('Loaded input file ...')
93-
fs.readFile(path_template, function(err, template) {
94-
template = eval('template = ' + template)
95-
console.log('Instantiate template ...')
90+
function render_template(path_template) {
91+
var template = fs.readFileSync(path_template).toString()
92+
template = eval('template = ' + template)
93+
console.log('Instantiate template ...')
94+
return function (input_data) {
9695
out = jsonT(input_data, template)
9796
console.log('Parsing as JSON ...')
9897
try {
@@ -115,13 +114,20 @@ fs.readFile(path_input, function(err, input_data) {
115114
catch(e) {
116115
console.log('JSON parsing failed' + e.message)
117116
}
118-
fs.writeFile(path_output, out.toString(), function(err) {
119-
if (err) {
120-
console.log(err)
121-
return
122-
}
123-
console.log('Success!')
124-
})
117+
return out
118+
}
119+
}
120+
121+
fs.readFile(path_input, function(err, input_data) {
122+
input_data = JSON.parse(input_data)
123+
console.log('Loaded input file ...')
124+
var out = render_template(path_template)(input_data)
125+
fs.writeFile(path_output, out.toString(), function(err) {
126+
if (err) {
127+
console.log(err)
128+
return
129+
}
130+
console.log('Success!')
125131
})
126132
})
127133

0 commit comments

Comments
 (0)