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 = const template = 'this is an example, published at {{timestamp}}' const dataSource = timestamp: Date const newString = ; console //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 = console //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; //console panel will see: //>> the double backslashes "\"
Run Test
npm install
npm run test