• Build the content by given template, data and options.

    Parameters

    • template: string

      The template string.

    • data: Record<string, unknown>

      The data to fill in the template.

    • Optional options: {
          missingDataBehavior?: "throw" | "ignore";
      }

      The options to build the content.

      • Optional missingDataBehavior?: "throw" | "ignore"

    Returns string

    The built content.

    Description

    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.

    Example

    const template = 'Hello, {{name}}!';
    const data = { name: 'World' };
    const content = buildContentFromTemplateString(template, data);
    // content = 'Hello, World!'

    Example

    const template = 'Hello, {{withDefault user.customFields.familyName "world"}}!';
    const data = { };
    const content = buildContentFromTemplateString(template, data, { missingDataBehavior: 'throw' });
    // content = 'Hello, World!'

    Example

    const template = 'Hello, {{name}}!';
    const data = { };
    const content = buildContentFromTemplateString(template, data, { missingDataBehavior: 'ignore' });
    // content = 'Hello, !'

    Example

    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!'

    Link

    https://handlebarsjs.com/

Generated using TypeDoc