# `Macaw.Template`
[🔗](https://github.com/dcoai/macaw/blob/v0.6.0/lib/macaw/template.ex#L1)

A template (spec/06 §2.2): `%Macaw.Template{name, regions, order, layout, params,
print}`, a registered, named arrangement of regions.

  * `name`, its name, lower-case words joined by `_`;
  * `regions`, its `%Macaw.Region{}`s by name, and `order`, their DOM order, the order
    they were given in;
  * `layout`, where its regions sit on the screen: `:stack`, one column in their order,
    or `{:areas, options}`, the areas layout's template naming regions;
  * `params`, its options by name, each a parameter (`Macaw.Data.Parameter`);
  * `print`, the name of its own print layer, or `nil` for the automatic one.

Templates and layers are written in code and registered by their package:

    defmodule MyApp.Site do
      use Macaw.Template

      template :site do
        region :header, role: :banner
        region :main, role: :main, fillable: true
        param :sidebar, values: [:shown, :hidden], default: :shown
      end
    end

`stack/2` stacks a template and its layers through macaw_layers' fragment kernel, and
validates the result whole; `explain/1` says which layer set each value and which masks
dropped what.

# `t`

```elixir
@type t() :: %Macaw.Template{
  layout: :stack | {:areas, map()},
  name: String.t(),
  order: [String.t()],
  params: %{required(String.t()) =&gt; Macaw.Data.Parameter.t()},
  print: String.t() | nil,
  regions: %{required(String.t()) =&gt; Macaw.Region.t()}
}
```

# `explain`

```elixir
@spec explain(Macaw.Template.Stacked.t()) :: %{leaves: [map()], masks: [map()]}
```

A stacked template's provenance: `%{leaves: leaves, masks: masks}`. `leaves` are
`Macaw.Layers.Fragment.explain/2`'s entries over the masked layers, each leaf's `path`,
`value`, the `layer` that set it and the layers it `overrode`; `masks` one entry per
mask path of each layer, `%{layer:, path:, dropped:}`, the layers below whose value at
the path it dropped, lowest first.

# `new`

```elixir
@spec new(map() | keyword()) :: {:ok, t()} | {:error, [Macaw.Base.Fault.t(), ...]}
```

A template from a keyword list or a map with atom keys: `{:ok, template}` or every fault
at once by path from the template. `name` is `:required` or `{:type, :name}`;
`regions` that isn't a non-empty list is `{:type, :regions}`, a region's faults are at
`[:regions, i | path]` and a name given twice is `{:duplicate, name}` at `[:regions, i,
:name]`; a layout that is neither `:stack` nor `{:areas, options}` is `{:type, :layout}`,
an areas template's faults as the areas layout gives them; a param's faults are
`Macaw.Data.Parameter.check/1`'s at `[:params, name | path]`, its `type` defaulting to
`:category`; a `print` that isn't a name is `{:type, :name}`; another key is
`:unknown_key`.

    iex> {:ok, t} = Macaw.Template.new(name: :site, regions: [[name: :main, role: :main]])
    iex> {t.name, t.order, t.layout}
    {"site", ["main"], :stack}

# `placement`

```elixir
@spec placement(Macaw.Template.Stacked.t(), String.t() | atom()) ::
  {:ok, keyword()} | {:error, [Macaw.Base.Fault.t(), ...]}
```

A region's placement (spec/06 §2.6): `{:ok, [kind: kind, kinds: kinds]}`, the kind a
cell in the region takes when it names none and the kinds it allows, which
`Macaw.Surface.validate/4` takes as its options; or `{:error, [{[:region],
{:unknown_region, name}}]}`.

# `stack`

```elixir
@spec stack([String.t() | atom()], Macaw.Registry.t()) ::
  {:ok, Macaw.Template.Stacked.t()} | {:error, [Macaw.Base.Fault.t(), ...]}
```

The template stack `names` stacked (spec/06 §2.4): the template first, then its layers,
lowest first, each a string or an atom read as its spelling, looked up in the host's
registry. `{:ok, stacked}`, the template with its layers applied and validated whole, or
`{:error, faults}`, every fault at once, each naming the layer at fault: its path starts
with the layer's name (the template's for the template), then the path in the stacked
template.

This is the seam with the page: a page stores its stack's names and calls `stack/2`.

# `stack`

```elixir
@spec stack([String.t() | atom()], Macaw.Registry.t(), keyword()) ::
  {:ok, Macaw.Template.Stacked.t()} | {:error, [Macaw.Base.Fault.t(), ...]}
```

`stack/2` with the page's own last layer and print (spec/06 §2.5, §2.7):

  * `own:`, a map or keyword list of `params:` (values by name) and `regions:` (each
    region's `content:` only), stacked above the registered layers. A key other than
    those is `{[:own, key], :not_allowed}`, a region not declared fillable in the stack
    below `{[:own, :regions, region], :not_fillable}`, a region key other than `content`
    `{[:own, :regions, region, key], :not_allowed}`, an undeclared param
    `{[:own, :params, param], :unknown_param}`. It is the page's last word, so it never
    collides; its faults name it `:own`.
  * `print: true` stacks print last: the template's own `print:` layer, or the automatic
    one hiding every region but `main` and opening disclosures, named `:print`.

Another option is `{[:opts, key], :unknown_key}`, a `print:` that isn't a boolean
`{[:opts, :print], {:type, :boolean}}`, and options that aren't a keyword list
`{[:opts], {:type, :keyword}}`.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
