Templating engines turn a string like Hello {{name}}! into rendered output by replacing placeholders with data.
In this question, implement renderTemplate(template, data), a simplified Mustache-like renderer.
This question is intentionally small:
{{name}} and {{ name }} need to be supported.data is a flat object of primitive values.renderTemplate('Hello {{name}}!', { name: 'Alice' });// 'Hello Alice!'renderTemplate('{{greeting}}, {{name}}!', {greeting: 'Hi',name: 'Sam',});// 'Hi, Sam!'
Whitespace inside tags should be ignored.
renderTemplate('{{ count }} items left', { count: 3 });// '3 items left'
Missing keys should render as empty strings.
renderTemplate('Hello {{name}} {{surname}}!', { name: 'Alice' });// 'Hello Alice !'
renderTemplate(template, data) accepts the following arguments:
| Argument | Type | Description |
|---|---|---|
template | string | The template string containing {{...}} placeholders. |
data | Object | A flat object whose values are primitive values. |
Returns a new string with all placeholders replaced.
false, 0, and '' are valid values and should still render.null and undefined values should render as an empty string.console.log() 语句将显示在此处。