Typescript library that handles basic functions of the golang template syntax.
npm install @ctrl/golang-template
import { parse } from '@ctrl/golang-template';
const keywords = '123';
parse('{{ if .keywords }}{{ .keywords }}!!{{else}}nothing{{end}}', { keywords });
// '123!!'
`{{ .foo }}`
`{{ if .keywords }}{{ .keywords }}{{else}}nothing{{end}}`
const categories = ['1', '2', '3'];
parse('{{ join .categories "," }}', { categories });
// 1,2,3
const categories = ['1', '2', '3'];
parse('{{ range .categories }}{{.}};{{end}}', { categories });
// 1;2;3;
const categories = ['1', '2', '3'];
parse('{{ re_replace .category "[^a-zA-Z0-9]+" "%" }}', { categories });
// 1;2;3;
const data = {
object: {
value: 'foo'
},
array: [
'bar'
]
}
parse('{{ index .object "value" }}', data);
//foo
parse('{{ index .array 0 }}', data);
//foo
This is probably not safe for user input.