# `Braces.Liquid`
[🔗](https://github.com/dbernheisel/braces/blob/v0.1.0/lib/braces/liquid.ex#L1)

Liquid templates, rendered as Liquid renders them.

    iex> Braces.Liquid.render_to_string("Hi {{ name | upcase }}!", %{"name" => "<ada>"})
    {:ok, "Hi &lt;ADA&gt;!"}

    iex> Braces.Liquid.render_to_string("{% for p in people reversed %}{{ forloop.index }}. {{ p.name | default: 'someone' }} {% endfor %}", %{"people" => [%{"name" => "ada"}, %{}]})
    {:ok, "1. someone 2. ada "}

## Rendering

`render/3` renders a template with its data. `compile/2` compiles one to render many times,
and the `~LIQUID` sigil compiles it with your code, so a template that can't be rendered is
a `CompileError` at its line:

    import Braces.Liquid, only: :sigils
    Braces.render(~LIQUID"Hi {{ name }}", data)

`Braces.Liquid.Phoenix.Engine` renders `.liquid` files in Phoenix. `Braces.convert/2`
converts Liquid to and from the other languages.

## What renders

  * Values: `{{ user.name }}`, `{{ user["first name"] }}`, `{{ items[0] }}`,
    `{{ items.size }}`, `.first`, `.last` and `.length`, a string's characters, `{{ s[0] }}`,
    with filters: `{{ name | upcase }}`,
    `{{ price | times: 2 }}`, `{{ name | default: "friend" }}`.
  * `{% if %}`, `{% elsif %}`, `{% else %}` and `{% unless %}`, with `==`, `!=`, `<>`, `<`,
    `>`, `<=`, `>=`, `contains`, `and` and `or`, grouped from the right as Liquid groups
    them, and comparisons with `empty` and `blank`.
  * `{% case %}` with `{% when a, b or c %}` and `{% else %}`, rendering each `when` that
    matches, as LiquidJS does.
  * `{% for item in items %}` with `{% else %}`, `reversed`, `limit:`, `offset:` and
    `offset: continue`, over a
    list or a range, `(1..5)`; `forloop.index`, `index0`, `rindex`, `rindex0`, `first`,
    `last`, `length` and `parentloop`; `{% break %}` and `{% continue %}`.
  * `{% tablerow %}` with `cols:`, `limit:` and `offset:`, and `tablerowloop`;
    `{% cycle %}`, with or without a group; `{% increment %}` and `{% decrement %}`.
  * `{% assign %}`, `{% capture %}` and `{% echo %}`, and `{% liquid %}`, a tag per line.
  * `{% raw %}`, `{% comment %}`, `{% # inline comments %}` and `{% doc %}`, and whitespace
    control, `{{-` and `-%}`.
  * `{% layout "base" %}` and `{% block %}`, with `{{ block.super }}`, and
    `{% include "card" %}` and `{% render "card", key: value %}`, from the templates given as
    `templates:`. See [Layouts and includes](layouts.md).

Filters are LiquidJS's standard filters, Jekyll's among them: `where`, `map` and `sort` by a
key path, `"a.b"` or `'a["b c"]'`; `where_exp`, `reject_exp`, `find_exp`, `find_index_exp`,
`has_exp` and `group_by_exp`, whose expression in a string is read with the template;
`slugify`, `number_of_words` with `"cjk"`, `date_to_xmlschema`, `date_to_rfc822`,
`date_to_string`, `date_to_long_string` and `sample`. Give `helpers:` for others. A filter
Braces doesn't know gives its input, as in LiquidJS. Only `nil` and `false` are false, and
values are compared and written as LiquidJS does, as JavaScript would. `date` writes times
in UTC, at an offset given in minutes, or in a time zone named, `"America/New_York"`, when
Elixir has a time zone database, such as `tzdata`'s, configured.

Values are HTML-escaped, where Liquid writes them as they are, except those written with
`escape` or `escape_once`, and the markup a `capture` holds. Pass `escape: false` to write
them as Liquid does.

`ifchanged`, `render … for`, filters' named arguments other than `default`'s `allow_false`,
and a template named by a variable are a `Braces.ConversionError` with the line and column.
Converting to another language, `cycle`, `increment`, `decrement`, `tablerow`, `break`,
`continue` and the filters of an expression are a `Braces.ConversionError`, as other
languages have nothing like them.

# `compile`

```elixir
@spec compile(String.t(), keyword()) ::
  {:ok, Braces.Template.t()} | {:error, Exception.t()}
```

Compiles Liquid into a `Braces.Template` for `render/3`, or gives the error that
`convert/2` gives.

  * `escape: false` writes values without HTML escaping.
  * `templates:` the templates it extends or includes, by name: a map of names to
    Liquid source or `Braces.Template`s, or a function of a name giving `{:ok, template}`
    or `:error`. They're compiled into it.

# `compile!`

```elixir
@spec compile!(String.t(), keyword()) :: Braces.Template.t()
```

Like `compile/2`, raising the error.

# `render`

```elixir
@spec render(String.t() | Braces.Template.t(), term(), keyword()) ::
  {:ok, iodata()} | {:error, Exception.t()}
```

Renders Liquid, or a `Braces.Template` of it, with `data`, to iodata.

Takes `helpers:`, functions for filters Braces doesn't have, as modules that use
`Braces.Handlebars.Helpers`, and, with source, `escape:` and `templates:`, as
`compile/2` does.

# `render_to_string`

```elixir
@spec render_to_string(String.t() | Braces.Template.t(), term(), keyword()) ::
  {:ok, String.t()} | {:error, Exception.t()}
```

Like `render/3`, giving a string.

# `sigil_LIQUID`
*macro* 

The `~LIQUID` sigil: Liquid compiled with your code into a `Braces.Template`, so a
template that doesn't convert is a `CompileError` at its line. Import it with
`import Braces.Liquid, only: :sigils`.

# `convert`

Converts Liquid to Handlebars source, or gives a `Braces.ConversionError` with the line
and column of what can't be converted. Takes `escape: false` to write values without HTML
escaping, as Liquid does.

# `convert!`

Like `convert/2`, raising `Braces.ConversionError`.

# `from_ir`

Writes a `Braces.IR` as Liquid, or gives a `Braces.ConversionError` for what Liquid can't
say, such as helpers with no Liquid filter.

# `to_ir`

Reads Liquid into a `Braces.IR`, for `Braces.convert/2`: values are HTML-escaped unless
`escape: false` is given. Filters become `Braces.Filters` calls.

---

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