The template string.
The data to fill in the template.
Optional
options: { The options to build the content.
Optional
missingThe built content.
The template string is a string with placeholders. Please follow the handlebar syntax. The data is an object with key-value pairs. The key is the placeholder name, and the value is the value to fill in. The options defines the behavior of how template engine builds the content. It have 1 options: missingDataBehavior. missingDataBehavior defines the behavior when the placeholder data is missing. It can be 'throw', 'ignore'. If it is 'throw', the function will throw an error when the placeholder data is missing and no default value provided.
User can use {{withDefault value defaultValue}}
to set default value for placeholder.
const template = 'Hello, {{name}}!';
const data = { name: 'World' };
const content = buildContentFromTemplateString(template, data);
// content = 'Hello, World!'
const template = 'Hello, {{withDefault user.customFields.familyName "world"}}!';
const data = { };
const content = buildContentFromTemplateString(template, data, { missingDataBehavior: 'throw' });
// content = 'Hello, World!'
const template = 'Hello, {{name}}!';
const data = { };
const content = buildContentFromTemplateString(template, data, { missingDataBehavior: 'ignore' });
// content = 'Hello, !'
const template = 'Hello, {{withDefault user.customFields.familyName "world"}}!';
const data = {
user: {
customFields: {
familyName: 'bar',
givenName: 'foo',
},
name: 'foobar',
},
};
const content = buildContentFromTemplateString(template, data, { missingDataBehavior: 'throw' });
// content = 'Hello, world!'
Generated using TypeDoc
Build the content by given template, data and options.