Compiles substitution templates of the form "{0} something {1}"
into token lists for efficient parameter substitution at runtime.
Templates are parsed once into a list of string literals and integer indices. At runtime, values are substituted for the integer indices to produce the final output.
Summary
Functions
Parses a substitution template into a list of tokens.
Substitutes values into a pre-parsed template token list.
Functions
Parses a substitution template into a list of tokens.
Arguments
templateis a binary string that may include parameter markers like{0},{1}, etc.
Returns
A list of tokens where substitution markers become integers and literal text remains as strings.
{:error, reason}if the template is not a binary.
Examples
iex> Localize.Substitution.parse("{0}, {1}")
[0, ", ", 1]
iex> Localize.Substitution.parse("{0} something {1} else {2}")
[0, " something ", 1, " else ", 2]
iex> Localize.Substitution.parse("")
[]
Substitutes values into a pre-parsed template token list.
Arguments
valuesis a value or list of values to substitute into the template.tokensis a template token list previously created byparse/1.
Returns
- A list with values substituted for integer indices in the template.
Examples
iex> template = Localize.Substitution.parse("{0} and {1}")
[0, " and ", 1]
iex> Localize.Substitution.substitute(["a", "b"], template)
["a", " and ", "b"]
iex> Localize.Substitution.substitute("x", [0, "!"])
["x", "!"]