Hierarchy

  • default

Constructors

  • Creates an instance of the template engine.

    Example

    import MigascTemplate from 'migasc-template';

    const template = new MigascTemplate(
    // MigascTemplate Options
    { doAllowWhitespace: true },
    // MigascTemplate Dictionary
    {
    adjective: 'mysterious',
    animal: 'Cats',
    author: 'Sir Walter Scott',
    }
    );

    const template = '{{ animal }} are a {{ adjective }} kind of folk - {{ author }}';

    template.compile(template);
    // -> Cats are a mysterious kind of folk - Sir Walter Scott

    Parameters

    • Optional options: MigascTemplateOptions

      Configuration object defining the template language limitations.

    • Optional dict: Dictionary

      Initialize the dictionary object using the key:value pair structure.

    Returns default

Properties

_dict: Dictionary

A dictionary of replacement values using the key:value pair structure.

_dict404: string

Default replacement value for template matches not defined in the dictionary.

_engine: ((template: string, dict: Dictionary) => string)

Type declaration

    • (template: string, dict: Dictionary): string
    • Templating engine to produce the compiled string.

      Returns

      The compiled string.

      Parameters

      • template: string

        Template.

      • dict: Dictionary

        Dictionary.

      Returns string

_options: MigascTemplateOptions = ...

Configuration object defining the template language limitations.

_regexp: RegExp

Regular expression used to replace template text.

Default Value

/{{([a-zA-Z0-9_-]{1,64})}}/g

_template: string = ''

Raw template.

Default Value

''

_templateMap: TemplateMap = {}

Template map.

Default Value

{}

_templateModel: TemplateModel = []

Template model.

Default Value

[]

Methods

  • Map the given template.

    Parameters

    • template: string

      Template.

    Returns void

  • Templating engine to produce the compiled string using a precompiled map and model.

    Returns

    The compiled string.

    Parameters

    Returns string

  • Compile a given template into plain text using a defined dictionary. If dict is omitted, the instance global dictionary will be used.

    Returns

    The compiled string.

    Example

    Use the global dictionary.

    import MigascTemplate from 'migasc-template';

    const mt = new MigascTemplate(
    // MigascTemplate Options
    { doAllowWhitespace: true },
    // MigascTemplate Dictionary
    {
    adjective: 'mysterious',
    animal: 'Cats',
    author: 'Sir Walter Scott',
    }
    );

    const template =
    '{{ animal }} are a {{ adjective }} kind of folk - {{ author }}';

    mt.compile(template);
    // -> Cats are a mysterious kind of folk - Sir Walter Scott

    Example

    Use a local dictionary.

    It is possible to merge the global and local dictionaries.

    import MigascTemplate from 'migasc-template';

    const mt = new MigascTemplate(
    // MigascTemplate Options
    { doAllowWhitespace: true },
    // MigascTemplate Dictionary
    {
    adjective: 'mysterious',
    animal: 'Cats',
    author: 'Sir Walter Scott',
    }
    );

    const template =
    '{{ animal }} are a {{ adjective }} kind of folk - {{ author }}';

    mt.compile(template);
    // -> Cats are a mysterious kind of folk - Sir Walter Scott

    const localDict = {
    author: 'Michael Scott',
    };

    mt.compile(template, { ...mt.getDict(), ...localDict });
    // -> Cats are a mysterious kind of folk - Michael Scott

    Parameters

    • template: string

      Template.

    • Optional dict: Dictionary

      Local dictionary (supersedes global dictionary).

    Returns string

  • Return the current global dictionary.

    Remarks

    Property accessors (getters/setters) are not used for ES3 compatibility.

    Returns

    The global dictionary.

    Example

    import MigascTemplate from 'migasc-template';

    const mt = new MigascTemplate(
    // MigascTemplate Options
    { doAllowWhitespace: true },
    // MigascTemplate Dictionary
    {
    adjective: 'mysterious',
    animal: 'Cats',
    author: 'Sir Walter Scott',
    }
    );

    const template = '{{ animal }} are a {{ adjective }} kind of folk - {{ author }}';

    const localDict = {
    author: 'Michael Scott',
    };

    mt.compile(template, {
    ...mt.getDict(),
    ...localDict,
    });
    // -> Cats are a mysterious kind of folk - Michael Scott

    Returns Dictionary

  • Return the current default value for any undefined dictionary values.

    Remarks

    Property accessors (getters/setters) are not used for ES3 compatibility.

    Returns

    The default value for any undefined dictionary values.

    Example

    import MigascTemplate from 'migasc-template';

    const mt = new MigascTemplate({
    dict404: '__NOT_FOUND__',
    });

    mt.getDict404();
    // -> __NOT_FOUND__

    Returns string

  • Return the current raw template.

    Remarks

    Get the current instance template as is from input.

    Property accessors (getters/setters) are not used for ES3 compatibility.

    Returns

    The raw template.

    Example

    import MigascTemplate from 'migasc-template';

    const mt = new MigascTemplate();

    const template =
    '{{ animal }} are a {{ adjective }} kind of folk - {{ author }}';

    mt.setTemplate(template);

    mt.getTemplate();
    // -> {{ animal }} are a {{ adjective }} kind of folk - {{ author }}

    Returns string

  • Update the global dictionary.

    Remarks

    The dict parameter is the new global dictionary of replacement values using the key:value pair structure.

    Property accessors (getters/setters) are not used for ES3 compatibility.

    Example

    import MigascTemplate from 'migasc-template';

    const mt = new MigascTemplate(undefined,
    // MigascTemplate Dictionary
    {
    adjective: 'mysterious',
    animal: 'Cats',
    author: 'Sir Walter Scott',
    }
    );

    const template = '{{ animal }} are a {{ adjective }} kind of folk - {{ author }}';

    mt.setDict({
    adjective: 'special',
    animal: 'Kevins',
    author: 'Michael Scott',
    });

    mt.compile(template);
    // -> Kevins are a special kind of folk - Michael Scott

    Parameters

    Returns void

  • Update the default value for any undefined dictionary values.

    Remarks

    Based off the well-known 404 HTTP error response status code - dict404 is the default replacement string for any undefined dictionary values.

    Property accessors (getters/setters) are not used for ES3 compatibility.

    Example

    import MigascTemplate from 'migasc-template';

    const mt = new MigascTemplate({
    dict404: '__NOT_FOUND__',
    });

    mt.setDict404('!FOUND');

    mt.getDict404();
    // -> !FOUND

    Parameters

    • dict404: string

      The new default value for any undefined dictionary values.

    Returns void

  • Precompile a template.

    Remarks

    Property accessors (getters/setters) are not used for ES3 compatibility.

    Example

    import MigascTemplate from 'migasc-template';

    const mt = new MigascTemplate(
    // MigascTemplate Options
    { doAllowWhitespace: true },
    // MigascTemplate Dictionary
    {
    adjective: 'mysterious',
    animal: 'Cats',
    author: 'Sir Walter Scott',
    }
    );

    const template =
    '{{ animal }} are a {{ adjective }} kind of folk - {{ author }}';

    mt.setTemplate(template);

    mt.template();
    // -> Cats are a mysterious kind of folk - Sir Walter Scott

    mt.getTemplate();
    // -> {{ animal }} are a {{ adjective }} kind of folk - {{ author }}

    Parameters

    • template: string

      Template.

    Returns void

  • Compile preset template into plain text using a defined dictionary. If dict is omitted, the instance global dictionary will be used.

    Returns

    The compiled string.

    Example

    Use the global dictionary.

    import MigascTemplate from 'migasc-template';

    const mt = new MigascTemplate(
    // MigascTemplate Options
    { doAllowWhitespace: true },
    // MigascTemplate Dictionary
    {
    adjective: 'mysterious',
    animal: 'Cats',
    author: 'Sir Walter Scott',
    }
    );

    const template =
    '{{ animal }} are a {{ adjective }} kind of folk - {{ author }}';

    mt.setTemplate(template);

    mt.template();
    // -> Cats are a mysterious kind of folk - Sir Walter Scott

    Example

    Use a local dictionary.

    It is possible to merge the global and local dictionaries.

    import MigascTemplate from 'migasc-template';

    const mt = new MigascTemplate(
    // MigascTemplate Options
    { doAllowWhitespace: true },
    // MigascTemplate Dictionary
    {
    adjective: 'mysterious',
    animal: 'Cats',
    author: 'Sir Walter Scott',
    }
    );

    const template =
    '{{ animal }} are a {{ adjective }} kind of folk - {{ author }}';

    mt.setTemplate(template);

    mt.template();
    // -> Cats are a mysterious kind of folk - Sir Walter Scott

    const localDict = {
    author: 'Michael Scott',
    };

    mt.template({ ...mt.getDict(), ...localDict });
    // -> Cats are a mysterious kind of folk - Michael Scott

    Parameters

    • Optional dict: Dictionary

      Local dictionary (supersedes global dictionary).

    Returns string