Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- export class TemplateEngine {
- /**
- * Template engine.
- * @see https://stackoverflow.com/questions/30003353/can-es6-template-literals-be-substituted-at-runtime-or-reused
- * @see https://gist.github.com/tmarshall/31e640e1fa80c597cc5bf78566b1274c
- *
- * @param templateString The template string, e.g "Hello ${name}"
- * @param templateVars Map with variables, e.g { name: "John" }
- * @returns The template string with variables replaced, e.g "Hello John"
- */
- static fillTemplate(templateString: string, templateVars: any): string {
- const handler = new Function(
- 'vars',
- [
- 'const tagged = ( ' + Object.keys(templateVars).join(', ') + ' ) =>',
- '`' + templateString + '`',
- 'return tagged(...Object.values(vars))',
- ].join('\n'),
- );
- return handler(templateVars);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement