Marea.Templates (marea v0.0.1-rc.1)

Copy Markdown View Source

Template registry, embedded at compile time.

All regular files under priv/templates/<prefix>/... are read into the module at compile time. A template's name is its path relative to priv/templates/, e.g. "docker/dockerfile_v01.eex", "helm/configmap_env.yaml.eex", "rel/env.sh.eex".

Prefixes

Files are organised by a single top-level directory under priv/templates/, called the prefix. Some prefixes have a default file extension that callers may omit:

  • helm.yaml.eex
  • docker.eex
  • rel.eex

So get("rel/env.sh") and get("rel/env.sh.eex") resolve to the same template; get("helm/configmap_env") resolves to helm/configmap_env.yaml.eex. As a convenience, callers that pass the rendered-output name (e.g. get("helm/configmap_env.yaml")) also resolve to the .eex source, so user-facing config files can keep referring to templates by their output extension. Prefixes without a default (e.g. k8s) always require the full filename.

EEx helpers

Built-in templates import Marea.Templates, only: [...] to call yaml!/1, indent/2 and to_dashes/1 from EEx.

Summary

Functions

Renders the template named name as EEx with assigns. Raises if the template is not embedded.

Renders raw EEx content with assigns.

Returns the template content, or nil if missing.

Same as get/1 but raises if the template is missing.

Indents every line of string by count spaces.

Lists template names whose path starts with prefix <> "/".

All embedded templates as %{relative_path => content}.

Replaces _ with - for use in template-generated identifiers.

Parses a single YAML document; returns [] for empty input.

Functions

expand(name, assigns \\ [])

@spec expand(
  String.t(),
  keyword()
) :: String.t()

Renders the template named name as EEx with assigns. Raises if the template is not embedded.

expand_content(content, assigns, name \\ "none")

@spec expand_content(String.t(), keyword(), String.t()) :: String.t()

Renders raw EEx content with assigns.

Used when the source isn't a built-in template (e.g. a user-supplied file under <marea_dir>/templates/). name is used only for EEx error messages. Whitespace-only lines in the result are collapsed to keep generated YAML clean.

get(name)

@spec get(String.t()) :: String.t() | nil

Returns the template content, or nil if missing.

name may include the file extension (e.g. "rel/env.sh.eex") or omit it for a known prefix (e.g. "rel/env.sh").

get!(name)

@spec get!(String.t()) :: String.t()

Same as get/1 but raises if the template is missing.

indent(string, count)

@spec indent(String.t(), non_neg_integer()) :: String.t()

Indents every line of string by count spaces.

list(prefix)

@spec list(String.t()) :: [String.t()]

Lists template names whose path starts with prefix <> "/".

Returns the relative paths (with extension), suitable for passing back to get/1 or expand/2.

templates()

@spec templates() :: %{optional(String.t()) => String.t()}

All embedded templates as %{relative_path => content}.

to_dashes(value)

@spec to_dashes(atom() | charlist() | String.t()) :: String.t()

Replaces _ with - for use in template-generated identifiers.

yaml!(string)

@spec yaml!(String.t()) :: term()

Parses a single YAML document; returns [] for empty input.