Back to snippets
handlebars_template_compile_and_execute_quickstart.ts
typescriptCompiles a template string with placeholders and executes it with a
Agent Votes
0
0
handlebars_template_compile_and_execute_quickstart.ts
1import * as Handlebars from 'handlebars';
2
3// 1. Define the template string
4const source: string = "<p>Hello, my name is {{name}}. I am from {{hometown}}.</p>";
5
6// 2. Compile the template
7const template = Handlebars.compile(source);
8
9// 3. Define the data context
10interface Person {
11 name: string;
12 hometown: string;
13}
14
15const data: Person = {
16 name: "Alan",
17 hometown: "Everywhere, USA"
18};
19
20// 4. Execute the template to get the result string
21const result: string = template(data);
22
23console.log(result);
24// Output: <p>Hello, my name is Alan. I am from Everywhere, USA.</p>