# Compiled templates

A template written in your code, or kept in a file next to it, is compiled into Elixir code
when your module compiles, as HEEx compiles `~H`. Mistakes are caught at compile time, and
rendering skips reading the AST.

```elixir
defmodule MyApp.Emails do
  use Braces.Handlebars

  # lib/my_app/emails/welcome.hbs becomes welcome(data) and welcome(data, opts)
  embed_templates "emails/*.hbs"

  def reminder(data), do: Braces.Handlebars.render(~HBS"Hi {{first_name}}!", data)
end

MyApp.Emails.welcome(%{first_name: "Ana", items: [%{name: "Tea & cake"}]})
#=> iodata

MyApp.Emails.welcome(%{})
#=> ** (Braces.Handlebars.RenderError) "first_name" not defined in data - 1:5 …

MyApp.Emails.welcome(%{}, strict: false)
#=> iodata, with "" for first_name
```

`use Braces.Handlebars` imports `Braces.Handlebars.embed_templates/2` and `~HBS` (`Braces.Handlebars.Sigil`).

## `~HBS`

`~HBS` gives a `Braces.Handlebars.Template`, as `Braces.Handlebars.compile/2` does, with a `render`
function generated from it, which `Braces.Handlebars.render/3` runs. In a module the function is a
hidden function of the module, named after a hash of its code, so the template can be kept in a
module attribute. While the module compiles, before that function exists, `render/3` reads the
template's AST instead, even with an earlier version of the module loaded, as after IEx's
`r/1`.

## `embed_templates`

`embed_templates "emails/*.hbs"` turns each file that matches into a function named after the
file up to its first dot: `welcome.hbs` and `welcome.html.hbs` are `welcome/1` and
`welcome/2`. So `welcome.html.hbs` and `welcome.text.hbs` can't share a module: embed each
format in its own (see [the Swoosh guide](swoosh.md) for an example).

- They render to iodata and raise a `Braces.Handlebars.RenderError` for what can't be rendered, as
  `Braces.Handlebars.render!/3` does, the signature of the functions Phoenix's `embed_templates`
  defines.
- They render in strict mode unless you pass `strict: false`, so a missing value is an error,
  and they take `render/3`'s other options, such as `no_escape: true`.
- The pattern is relative to the module's file, or to `root: "…"`.
- Each file is an `@external_resource`, so editing one recompiles the module, as does adding or
  removing a file that matches.

## How compiled code renders

Compiled code renders exactly as `render/3` renders the AST, data, options, errors and carets
included. A path's segments are compiled to atoms, so `{{user.name}}` finds `:user` and `:name`
in atom-keyed data without looking atoms up at render time. A segment longer than an atom can
be (255 characters) is looked up as a string only, as at runtime.

## Compile errors

A template that doesn't parse, or that can't render whatever its data, is a `CompileError` at
its line in your file, or in the `.hbs` file, with the line and a caret under the error: a
syntax error, a built-in helper with the wrong number of arguments (`{{#if}}`, `{{#with a b}}`,
`{{#each}}`), `#if`, `#unless`, `#with` or `#each` without a block, decorators, and helpers and
partials that aren't defined (below).

```
** (CompileError) lib/my_app/emails/welcome.hbs:3: Handlebars template error: #if requires exactly one argument - 3:2

      {{#if}}x{{/if}}
      ^
```

## Formatting

`Braces.Handlebars.Formatter` is a `mix format` plugin for `.hbs` files and `~HBS` sigils:

```elixir
# .formatter.exs
[
  plugins: [Braces.Handlebars.Formatter],
  inputs: ["{lib,test}/**/*.{ex,exs,hbs}", "*.exs"]
]
```

It formats the inside of each tag, `{{ first_name }}` to `{{first_name}}`, and leaves the
text between tags as it is, since that text is what the template renders.

## Helpers and partials

`use Braces.Handlebars` takes the helpers and partials of the module's templates:

```elixir
defmodule MyApp.Emails do
  use Braces.Handlebars,
    helpers: MyApp.EmailHelpers,
    # lib/my_app/emails/partials/footer.hbs is {{> footer}}
    partials: "emails/partials/*.hbs"

  embed_templates "emails/*.hbs"
end
```

- `helpers:` is a module that uses `Braces.Handlebars.Helpers`, or a list of them. `partials:` is a
  wildcard relative to the module's file, and each file is a partial named after it up to its
  first dot. Partial files are compiled into the module too, and are `@external_resource`s, so
  editing, adding or removing one recompiles the module.
- The module's `embed_templates` and `~HBS` templates render with these helpers and
  partials only: `render/3` raises `ArgumentError` when given `helpers:` or `partials:` for
  them. A `~HBS` template in a module without `use Braces.Handlebars` has none.
- So what they call is known when the module compiles, and these are `CompileError`s with a
  caret, even in a branch that wouldn't render: a helper that isn't defined
  (`Missing helper: "shout"`), including in a subexpression or with only a hash; a helper
  called with a number of arguments it doesn't take (`Helper "shout" can't take 2 arguments`);
  and `{{> footer}}` when `footer` is neither a partial file nor an `{{#*inline}}` partial of
  the template (`The partial footer could not be found`). A partial file may call an inline
  partial that a template of the module defines, as a layout calls the `{{#*inline
  "content"}}` of the templates that use it.
- These are left to render time, as in Handlebars.js: a `{{> (lookup . "name")}}` partial,
  whose name comes from the data; `{{#> layout}}…{{/layout}}`, which renders its block when
  there's no `layout`; and, with a `helperMissing` helper, helpers that aren't defined.
