Engine

templeo~ Engine

new Engine(optsopt, readFormatteropt, writeFormatteropt, logopt)

Creates a template literal engine

Source:
Parameters:
Name Type Attributes Description
opts TemplateOpts <optional>

The TemplateOpts to use

readFormatter function <optional>

The function(string, readFormatOptions) that will return a formatted string for reading data using the options.readFormatOptions from TemplateOpts as the formatting options. Typically reads are for HTML minification and/or beautifying.

writeFormatter function <optional>

The function(string, writeFormatOptions) that will return a formatted string for writting data using the options.writeFormatOptions from TemplateOpts as the formatting options. Typically reads are for JS minification and/or beautifying.

log Object <optional>

The log for handling logging output

Name Type Attributes Description
debug function <optional>

A function that will accept debug level logging messages (i.e. debug('some message to log'))

info function <optional>

A function that will accept info level logging messages (i.e. info('some message to log'))

warn function <optional>

A function that will accept warning level logging messages (i.e. warn('some message to log'))

error function <optional>

A function that will accept error level logging messages (i.e. error('some message to log'))

Members

legacyRenderOptions

The LEGACY-ONLY API TemplateOpts to use when no rendering options are passed (or are empty) into the rendering function and a callback function is specified when calling Engine.compile

Source:

legacyRenderOptions

Source:

options

Source:

Methods

(static) create(cachier) → {Engine}

Creates a new Engine from a Cachier instance

Source:
Parameters:
Name Type Description
cachier Cachier

The Cachier to use for persistence management

Returns:
Type:
Engine

The generated Engine

(async) clearCache(allopt)

Clears the underlying cache

Source:
Parameters:
Name Type Attributes Default Description
all Boolean <optional>
false

true to clear ALL unassociated cache instances when possible as well as any partials that have been registered

(async) compile(contentopt, optsopt, paramsopt, legacyCallbackopt) → {function}

Compiles a template and returns a function that renders the template results using the passed context object

Source:
Parameters:
Name Type Attributes Description
content String | Boolean <optional>

The raw template content, true to read from cache before compilation. Omit to load the template content from cache when the returned rendering function is called.

opts Object <optional>

The options sent for compilation (omit to use the options set on the Engine)

params URLSearchParams <optional>

Any URL search parmeters that will be passed when capturing the primary template and/or context when needed. Parameters can be excluded from the invocation by replacing params with a callback (e.g. compile(content, opts, callback)).

legacyCallback function <optional>

Optional callback style support for LEGACY-ONLY APIs:
compile(content, opts, (error, (ctx, opts, cb) => cb(error, results)) => {}) or omit to run via await compile(content, opts). Omission will return the normal stand-alone renderer that can be serialized/deserialized. When a legacy callback function is specified, serialization/deserialization of the rendering function will not be possible! In legacy mode Engine.legacyRenderOptions will be used during any rendering call that does not pass rendering options or passes rendering options that does not contain any properties.

Returns:
Type:
function

The rendering async function that returns a template result string based upon the provided context. The following arguments apply:

  1. {Object} context The context JSON that can be used as data during rendering
  2. {TemplateOpts} [renderOptions] The rendering options that will superceed any compile-time options
  3. {Function} [readFormatter] The function that will format read partials during include discovery (if any). The formatting function takes 1 or 2 arguments with the first being the content that will be formatted and the second being the options.readFormatOptions. The returned result should be a valid string.
  4. {Function} [writeFormatter] The function that will format written sources during include discovery (if any). The formatting function takes 1 or 2 arguments with the first being the content that will be formatted and the second being the options.writeFormatOptions. The returned result should be a valid string.
  5. {Object} [sharedStore] An object used for in-memory storage space that can be shared between rendering functions. This ensures that updated data within a renderer execution will be retained between rendering calls from the same renderer or different renderers that are passed the same shared store.

(async) getRegistered(name, paramsopt, extensionopt) → {Object}

Retrieves a template, partial or context that resides in-memory.

Source:
Parameters:
Name Type Attributes Default Description
name String

The name that uniquely identifies the template, partial or context

params URLSearchParams <optional>

Any parameters designated during Engine.registerPartial

extension String <optional>
options.defaultExtension

Optional override for a file extension designation for the template, partial or context designated during Engine.registerPartial

Returns:
Type:
Object

A copy of the generated data from Engine.registerPartial

(async) register(dataopt, readopt, writeopt) → {Object}

Registers and caches the template, one or more partial templates and/or context JSON.

Source:
Parameters:
Name Type Attributes Description
data Array.<Object> <optional>

The template, partials and/or context to register.

partials[].name String

The name that uniquely identifies the template, partial or context

partials[].content String <optional>

The raw content that will be registered. Omit when read === true to read content from cache.

partials[].params URLSearchParams <optional>

The URLSearchParams that will be passed during the content read (ignored when content is specified).

partials[].extension String <optional>

Optional override for a file extension designated for a template, partial or context.

read Boolean <optional>

When true, an attempt will be made to also Cachier.read the template, partials and context that do not have a content property set.

write Boolean <optional>

When true, an attempt will be made to also Cachier.write the template, partials and context that have a content property set.

Returns:
Type:
Object

An object that contains the registration results:

  • data The object that contains the template, partial fragments and/or context that have been registered
    • name The name that uniquely identifies the template, partial or context
    • content The raw content of the template, partial or context
    • extension The template file extension designation
    • params The URLSearchParams passed during the initial content read
    • fromRead A flag that indicates that the data was set from a read operation
    • overrideFromFileRead A flag that indicates if the passed partial content was overridden by content from a file read
  • dirs Present only when file system back-end is used. Contains the directories/sub-directories that were created

registerHelper(func)

Registers a directive function that can be used within template interpolations

Source:
Parameters:
Name Type Description
func function

A named function that has no external scope dependencies/closures other than those exposed via templates during rendering

(async) registerPartial(name, contentOrParams, extensionopt) → {String}

Registers and stores a partial template in-memory. Use Engine.register to write/persist partials to cache (Cachier)

Source:
Parameters:
Name Type Attributes Default Description
name String

The template name that uniquely identifies the template content

contentOrParams String | URLSearchParams

Either the partial template content string to register or the URLSearchParams that will be passed during the content read

extension String <optional>
options.defaultExtension

Optional override for a file extension designation for the partial

Returns:
Type:
String

The partial content

(async) renderPartial(name, contextopt) → {String}

On-Demand compilation of a registered templates

Source:
Parameters:
Name Type Attributes Default Description
name String

The name of the registered tempalte

context Object <optional>
{}

The object that contains contextual data used by the template

Returns:
Type:
String

The rendered template

renderPartialGenerate() → {function}

Source:
Returns:
Type:
function

A reference safe async function to Engine.renderPartial that can be safely passed into other functions

(async) unregister(name)

Unregisters a template, partial or context from cache

Source:
Parameters:
Name Type Description
name String

The name that uniquely identifies the template, partial or context


1.0.0 (2019-12-16)

Full Changelog