Optional
options: MigascTemplateOptionsConfiguration object defining the template language limitations.
Optional
dict: DictionaryInitialize the dictionary object using the key:value pair structure.
Private
_dictA dictionary of replacement values using the key:value pair structure.
Private
_dict404Default replacement value for template matches not defined in the dictionary.
Private
_engineTemplating engine to produce the compiled string.
The compiled string.
Template.
Dictionary.
Private
_optionsConfiguration object defining the template language limitations.
Private
_regexpRegular expression used to replace template text.
/{{([a-zA-Z0-9_-]{1,64})}}/g
Private
_templateRaw template.
''
Private
_templateTemplate map.
{}
Private
_templateTemplate model.
[]
Private
_mapPrivate
_templateTemplating engine to produce the compiled string using a precompiled map and model.
The compiled string.
Dictionary to use.
Compile a given template into plain text using a defined dictionary.
If dict
is omitted, the instance global dictionary will be used.
The compiled string.
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
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
Template.
Optional
dict: DictionaryLocal dictionary (supersedes global dictionary).
Return the current global dictionary.
Property accessors (getters/setters) are not used for ES3 compatibility.
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 }}';
const localDict = {
author: 'Michael Scott',
};
mt.compile(template, {
...mt.getDict(),
...localDict,
});
// -> Cats are a mysterious kind of folk - Michael Scott
Return the current default value for any undefined dictionary values.
Property accessors (getters/setters) are not used for ES3 compatibility.
The default value for any undefined dictionary values.
import MigascTemplate from 'migasc-template';
const mt = new MigascTemplate({
dict404: '__NOT_FOUND__',
});
mt.getDict404();
// -> __NOT_FOUND__
Return the current raw template.
Get the current instance template as is from input.
Property accessors (getters/setters) are not used for ES3 compatibility.
The raw template.
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 }}
Update the global dictionary.
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.
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
The new global dictionary.
Update the default value for any undefined dictionary values.
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.
import MigascTemplate from 'migasc-template';
const mt = new MigascTemplate({
dict404: '__NOT_FOUND__',
});
mt.setDict404('!FOUND');
mt.getDict404();
// -> !FOUND
The new default value for any undefined dictionary values.
Precompile a template.
Property accessors (getters/setters) are not used for ES3 compatibility.
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 }}
Template.
Compile preset template into plain text using a defined dictionary.
If dict
is omitted, the instance global dictionary will be used.
The compiled string.
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
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
Optional
dict: DictionaryLocal dictionary (supersedes global dictionary).
Creates an instance of the template engine.
Example