Options
All
  • Public
  • Public/Protected
  • All
Menu

Terminology

Let's get familiar with a few terms as they are used across this repo:

  • Path: is a string that is used in the template to refer to a value (Example: person.name)
  • Ref: is an array that is derived from a path (Example: ['person', 'name'])
  • Scope: The object that'll be used to resolve values (called view in Mustache.JS)
  • Parsing: converting a path to a ref that can be used to lookup a value from the Scope
  • Value: the value that will be used to interpolate a path in the template
  • Resolving: looking up the value for a path (either from the Scope or using your own resolver function)
  • Rendering: interpolating the Pathss in a template with their resolved value
  • Tokenization: breaking the template into constant strings and Paths that need resolving
  • Renderer: an object that holds a tokenized template and have methods for rendering
  • Compiling: tokenizing a template and creating a Renderer object
  • Stringification: putting the resolved Value for every path and creating the final string result (in compiler slang it is called generator)
  • Tag the strings that are used for marking paths in the template. By default they are '{{' and '}}' respectively

At a high level flow for a typical render looks like this:

1. render()
   |__ 2. compile()
          |__ 3. tokenize()
          |__ 4. resolve()
                 |__ 5. get() or resolveFn()
                 |__ 6. stringify()

Index

Type aliases

Ref

Ref: string[]

An array that is derived from a path string For example, if your template has a path that looks like 'person.name', its corresponding Ref looks like ['person', 'name']

ResolveFn

ResolveFn: (path: string, scope?: Scope) => any

The callback for resolving a value (synchronous)

param

the scope object that was passed to .render() function

param

a string that appeared in the string between open and close tags

example

a template that is Hi {{a.b.c}}! leads to 'a.b.c' as ref

returns

the value to be interpolated.

Type declaration

    • (path: string, scope?: Scope): any
    • Parameters

      • path: string
      • Optional scope: Scope

      Returns any

ResolveFnAsync

ResolveFnAsync: (path: string, scope?: Scope) => Promise<any>

Same as ResolveFn but for asynchronous functions

Type declaration

    • (path: string, scope?: Scope): Promise<any>
    • Parameters

      • path: string
      • Optional scope: Scope

      Returns Promise<any>

Functions

compile

  • Compiles a template and returns an object with functions that render it. Compilation makes repeated render calls more optimized by parsing the template only once and reusing the results. As a result, rendering gets 3-5x faster. Caching is stored in the resulting object, so if you free up all the references to that object, the caches will be garbage collected.

    throws

    TypeError if the template is not a string

    throws

    TypeError if the options is set but is not an object

    throws

    any error that tokenize or Renderer.constructor may throw

    Parameters

    • template: string

      same as the template parameter to .render()

    • Default value options: CompileOptions = {}

      some options for customizing the compilation

    Returns Renderer

    a Renderer object which has render methods

get

  • A useful utility function that is used internally to lookup a path in an object. It can also be used in your custom resolver functions if needed. Under the hood it uses getRef

    This is similar to Lodash's _.get()

    throws

    any error that getRef may throw

    Parameters

    • scope: Scope

      an object to resolve value from

    • path: string

      the path string as it appeared in the template

    • Default value options: GetOptions = {}

    Returns any

    the value or undefined

getRef

  • Looks up the value of a given Ref in the Scope It can also be used in your custom resolver functions if needed.

    see

    https://github.com/userpixel/micromustache/wiki/Known-issues If it cannot find a value in the specified ref, it may return undefined or throw an error depending on the value of the validateRef option

    throws

    any error that parsePath may throw

    throws

    SyntaxError if the path string cannot be parsed

    throws

    TypeError if the arguments have the wrong type

    throws

    ReferenceError if the scope does not contain the requested key and the validateRef is set to a truthy value

    Parameters

    Returns any

    the value or undefined

parsePath

  • parsePath(path: string): Ref
  • Breaks a path to an array of strings. The result can be used to get a particular value from a Scope object

    throws

    TypeError if the path is not a string

    throws

    SyntaxError if the path syntax has a problem

    Parameters

    • path: string

      the path as it occurs in the template. For example a["b"].c

    Returns Ref

    • an array of property names that can be used to get a particular value. For example ['a', 'b', 'c']

render

  • Replaces every {{path}} inside the template with values from the scope parameter.

    warning

    When dealing with user input, always make sure to validate it.

    throws

    any error that compile or Renderer.render may throw

    Parameters

    • template: string

      The template containing one or more {{path}} as placeholders for values from the scope parameter.

    • Optional scope: Scope

      An object containing values for paths from the the template. If it's omitted, we default to an empty object. Since functions are objects in javascript, the scope can technically be a function too but it won't be called. It'll be treated as an object and its properties will be used for the lookup.

    • Optional options: CompileOptions

      same options as the compile function

    Returns string

    Template where its paths replaced with corresponding values.

renderFn

  • Same as render but accepts a resolver function which will be responsible for returning a value for every path.

    throws

    any error that compile or Renderer.renderFn may throw

    Parameters

    • template: string
    • resolveFn: ResolveFn

      a function that takes a path and resolves it to a value. The value can be a number, string or boolean. If it is not, it'll be "stringified".

    • Optional scope: Scope
    • Optional options: CompileOptions

    Returns string

    Template where its paths are replaced with the values returned from the resolver function

renderFnAsync

  • Same as renderFn but supports asynchronous resolver functions (a function that returns a promise instead of the value).

    throws

    any error that compile or Renderer.renderFnAsync may throw

    Parameters

    Returns Promise<string>

    a promise that when resolved contains the template where its paths replaced with what is returned from the resolver function for each path.

tokenize

  • Parses a template and returns the tokens in an object.

    throws

    TypeError if there's an issue with its inputs

    throws

    SyntaxError if there's an issue with the template

    Parameters

    • template: string

      the template

    • Default value options: TokenizeOptions = {}

    Returns Tokens

    the resulting tokens as an object that has strings and paths