Template

public struct Template

A template that has already been compiled from a string containing double brace wrapped substitutions: {{ }}.

Compiling a template up-front (see TemplateProcessor.compile(_:)) parses the source once into a list of plain-text and substitution sections. process(context:) then only has to join those sections, substituting values for one or more contexts without re-scanning the source.

The substitution paths the template references are collected during compilation and exposed via substitutions, so callers can gather only the data the template actually needs.

  • The paths of all the substitutions found while compiling this template, in the order they appear. Substitutions whose path could not be parsed are not included.

    This is useful to know up-front which values a context needs to provide, so that (potentially slow) work to populate the context can be limited to only the required data.

    Declaration

    Swift

    public let substitutions: [JSONObjectPath]
  • Processes this compiled template against the given context, replacing every substitution with either the value extracted from the context, the substitution’s fallback, or a blank string "" when neither is available.

    Declaration

    Swift

    public func process(context: DataObject) -> String

    Parameters

    context

    the DataObject to extract substitution values from.

    Return Value

    A new string with all substitutions replaced.