Ootempl.Placeholder (ootempl v0.3.0)

Detects and parses placeholder variables in text.

Placeholders follow the format {{variable_name}} and support dot notation for nested data access (e.g., {{customer.name}}, {{order.items.0.price}}).

Filters

A placeholder may declare a chain of formatting filters after the variable, separated by | (Jinja/Liquid style). Each filter has a name and optional comma-separated arguments introduced with ::

{{ invoice.date | date: "%Y-%m-%d" }}
{{ total | round: 2 | currency: "USD" }}
{{ name | upcase }}

Whitespace around the variable, the | separators, and arguments is insignificant. Arguments may be double- or single-quoted strings, integers, or floats; bare words are treated as strings. See Ootempl.Filters for the built-in filters and how to register your own.

Escaping

To include literal {{ or }} in your document, use \{{ or \}}.

Examples

iex> Ootempl.Placeholder.detect("Hello {{name}}!")
[%{original: "{{name}}", variable: "name", path: ["name"], filters: []}]

iex> Ootempl.Placeholder.detect("{{customer.name}} ordered {{product.title}}")
[
  %{original: "{{customer.name}}", variable: "customer.name", path: ["customer", "name"], filters: []},
  %{original: "{{product.title}}", variable: "product.title", path: ["product", "title"], filters: []}
]

iex> Ootempl.Placeholder.detect("No placeholders here")
[]

Summary

Functions

Detects all placeholders in the given text.

Types

filter()

@type filter() :: %{name: String.t(), args: [term()]}

placeholder()

@type placeholder() :: %{
  original: String.t(),
  variable: String.t(),
  path: [String.t()],
  filters: [filter()]
}

Functions

detect(text)

@spec detect(String.t()) :: [placeholder()]

Detects all placeholders in the given text.

Returns a list of placeholder maps containing the original placeholder text, the variable name, the parsed path segments, and any parsed filters.

Escaped placeholders (preceded by backslash) are not detected.

Parameters

  • text - The text to scan for placeholders

Returns

  • A list of placeholder maps

Examples

iex> Ootempl.Placeholder.detect("Hello {{name}}")
[%{original: "{{name}}", variable: "name", path: ["name"], filters: []}]

iex> Ootempl.Placeholder.detect("{{a.b.c}}")
[%{original: "{{a.b.c}}", variable: "a.b.c", path: ["a", "b", "c"], filters: []}]

iex> Ootempl.Placeholder.detect(~S({{date | date: "%Y-%m-%d"}}))
[%{original: ~S({{date | date: "%Y-%m-%d"}}), variable: "date", path: ["date"], filters: [%{name: "date", args: ["%Y-%m-%d"]}]}]

iex> Ootempl.Placeholder.detect("Escaped \{{not_a_placeholder}}")
[]