TemplateInterpreter

open class TemplateInterpreter<T> : Interpreter

This interpreter is used to evaluate string expressions and return a transformed string, replacing the content where it matches certain patterns. Typically used in web applications, where the rendering of an HTML page is provided as a template, and the application replaces certain statements, based on input parameters.

  • The statements (patterns) registered to the interpreter. If found, these are going to be processed and replaced with the evaluated value

    Declaration

    Swift

    public let statements: [Pattern<T, TemplateInterpreter<T>>]
  • The context used when evaluating the expressions. These context variables are global, used in every evaluation processed with this instance.

    Declaration

    Swift

    public let context: Context
  • The StringTemplateInterpreter contains a TypedInterpreter, as it is quite common practice to evaluate strongly typed expression as s support for the template language. Common examples are: condition part of an if statement, or body of a print statement

    Declaration

    Swift

    public let typedInterpreter: TypedInterpreter
  • The evaluator type that is being used to process variables. By default, the TypedInterpreter is being used

    Declaration

    Swift

    public typealias VariableEvaluator = TypedInterpreter
  • The result type of a template evaluation

    Declaration

    Swift

    public typealias EvaluatedType = T
  • The evaluator, that is being used to process variables

    Declaration

    Swift

    public lazy var interpreterForEvaluatingVariables: TypedInterpreter { get set }
  • The statements, and context parameters are optional, but highly recommended to use with actual values. In order to properly initialise a StringTemplateInterpreter, you’ll need a TypedInterpreter instance as well.

    Declaration

    Swift

    public init(statements: [Pattern<T, TemplateInterpreter<T>>] = [],
                interpreter: TypedInterpreter = TypedInterpreter(),
                context: Context = Context())

    Parameters

    statements

    The patterns that the interpreter should recognise

    interpreter

    A TypedInterpreter instance to evaluate typed expressions appearing in the template

    context

    Global context that is going to be used with every expression evaluated with the current instance. Defaults to empty context

  • The main part of the evaluation happens here. In this case, only the global context variables are going to be used

    Declaration

    Swift

    public func evaluate(_ expression: String) -> T

    Parameters

    expression

    The input

    Return Value

    The output of the evaluation

  • The main part of the evaluation happens here. In this case, the global context variables merged with the provided context are going to be used.

    Declaration

    Swift

    open func evaluate(_ expression: String, context: Context) -> T

    Parameters

    expression

    The input

    context

    Local context that is going to be used with this expression only

    Return Value

    The output of the evaluation

  • Reduce block can convet a stream of values into one, by calling this block for every element, returning a single value at the end. The concept is usually used in functional environments

    Declaration

    Swift

    public typealias Reducer<T, K> = (_ existing: T, _ next: K) -> T

    Parameters

    existing

    The previously computed value. In case the current iteration is the first, it’s the inital value.

    next

    The value of the current element in the iteration

    Return Value

    The a combined value based on the previous and the new value

  • In order to support generic types, not just plain String objects, a reducer helps to convert the output to the dedicated output type

    Declaration

    Swift

    public typealias TemplateReducer = (initialValue: T, reduceValue: Reducer<T, T>, reduceCharacter: Reducer<T, Character>)

    Parameters

    initialValue

    based on the type, an initial value must to be provided which can serve as a base of the output

    reduceValue

    during template execution, if there is some template to replace, the output value can be used to append to the previously existing output

    reduceCharacter

    during template execution, if there is nothing to replace, the value is computed by the character-by-character iteration, appending to the previously existing output

  • The main part of the evaluation happens here. In this case, the global context variables merged with the provided context are going to be used.

    Declaration

    Swift

    public func evaluate(_ expression: String, context: Context = Context(), reducer: TemplateReducer) -> T

    Parameters

    expression

    The input

    context

    Local context that is going to be used with this expression only

    reducer

    In order to support generic types, not just plain String objects, a reducer helps to convert the output to the dedicated output type

    Return Value

    The output of the evaluation