tpl2
v1.0.2
Published
Replace the placeholder string with real data(e.g. url params,timestamp/version of copyright)
Downloads
5
Maintainers
Readme
tpl2
Replace the placeholder string with real data(e.g. url params,timestamp/version of copyright)
1, tpl2(templateString, dataSource)
- templateString:
Use double braces wrap the field that you want to replace,like {{field}}
.
Then tpl2
will replace {{field}}
with the value of dataSource, value = dataSource[field]
.
- dataSource:
The dataSource
is a tipical k-v Object
2, Sample
const tpl2 = require('tpl2')
const template = 'this is an example, published at {{timestamp}}'
const dataSource = {
timestamp: Date.now()
}
const newString = tpl2(template, dataSource);
console.log(newString)
//newString === 'this is an example, published at 1507889438310'
3, If you wanna keep the double braces, use backslash \
const template = 'the \\{\\{field\\}\\} wont be replaced by the dataSouce\'s field value'
const dataSource = {
field: "test_text"
}
const newString = tpl2(template, dataSource)
console.log(newString)
//newString === 'the {{field}} wont be replaced by the dataSouce's field value'
4, \\
=> \
The Unicode for \
is \u005c
var s = 'the double backslashes "\\\\"'; // or var s = 'the double backslashes \u005c\u005c'
console.log(tpl2(s, {}));
//console panel will see:
//>> the double backslashes "\"
Run Test
npm install
npm run test