Braces.IR (Braces v0.1.0)

Copy Markdown View Source

The template that every language converts to and from: each language's parser reads its source into a Braces.IR, and each language's printer writes one out, so Braces.convert/2 converts from any language to any other.

It holds what the languages have between them: output, conditions, loops, context changes, variables, and expressions of paths, literals, operators and filters. A printer writes what its language can say the same way, and gives a Braces.ConversionError for what it can't.

The template

  • body: the statements.
  • language: the language it was read from, such as :handlebars or :jinja, whose rules for writing values a printer follows where it can.
  • truth: which values the template's conditions take as false (see truth/0). Every condition, not, and, or, {% for %}…{% else %} and falsy default of the template uses it, so a printer for a language that decides otherwise writes the test out.

Statements

  • {:text, text}: text, written as it is.
  • {:output, expression, escape?}: a value, HTML-escaped when escape? is true.
  • {:if, [{condition, body}], else_body}: the body of the first condition that's true, or else_body, which may be nil.
  • {:for, list, bind, body, else_body, context?}: the body once for each element of list, or else_body when there are none. bind names the element, {:item, name}, or the key or index and the element, {:pair, key, value}, or is nil. When context? is true, the element becomes the context, as in Handlebars' #each and Go's range; when false, the context stays, as in Jinja and Liquid.
  • {:with, value, bind, body, else_body, context?}: when value is true, the body, with value named bind unless it's nil, and as the context when context? is true; otherwise else_body.
  • {:section, value, inverted?, body}: a Mustache section: the body once for each element of a list, once with a true value as the context, or, inverted, once when it's false.
  • {:assign, name, expression} and {:capture, name, body}: set a variable.
  • :break and :continue, in a loop.
  • {:let, [{name, value}], body}: body with variables set to the values, which have what they had before after it, as Jinja's and Django's {% with %}.
  • {:macro, name, params, body}: defines the macro name, whose params are [{name, default}], with nil for no default. It's called with :macro_call.
  • {:block, name, body}: a layout's block, which a template that extends the layout replaces with its own block of that name, or body. name is nil for the block a template's content outside its blocks goes in, as LiquidJS's {% block %} and Handlebars' {{> @partial-block}}.
  • :super, in a block: the block it replaces.
  • {:extends, name, body, rest}: the layout name names, with the blocks of body in place of its own. rest says what body has outside its blocks is: :ignored, but for the variables it sets, as in Jinja, or the layout's :block with no name.
  • {:include, name, bindings, scope}: the template name names, with bindings, [{name, expression}], set for it. scope says what it sees and sets: :context, the including template's variables, setting its own, as Jinja's include; :shared, setting them for the including template too, as Liquid's include; or :isolated, only its bindings, as Liquid's render.
  • {:at, %{line: line, column: column}, statement}: where a statement is in the source, for errors.

Expressions

  • {:literal, value}: a string, number, boolean or nil.
  • {:path, base, segments}: a value read from base through segments, each a string or an expression. base is :this, the context; {:up, n}, the context n changes out; :root, the template's data; {:local, name}, a variable; or :search, the first context out from this one that has the first segment, as Mustache looks names up.
  • {:get, value, segments}: a value read through segments from the value of an expression, as {'a': x}.a and (a ~ b).c.
  • {:data, name}: a Handlebars data variable, @name.
  • {:loop, field, up}: a loop's :index0, :index (from 1), :revindex0, :revindex (from 1), :first, :last, :length or :key, of the loop up loops out.
  • {:not, value}, {:and, a, b}, {:or, a, b}: by the template's truth, as Python's, giving one of the values for and and or.
  • {:default, value, fallback, mode}: fallback when value is :missing (nil), or :falsy (false by the template's truth), or :empty (nil, false, "", [] or %{}).
  • {:call, name, arguments, keyword_arguments}: a filter, function or operator, by its name in Braces.Filters: {{ a + b }} is {:call, "add", [a, b], []}. "range" is Python's range, which stops before its end, and "liquid_range" Liquid's, which ends with it.
  • {:cond, test, a, b}: a when test is true, otherwise b.
  • {:list, items} and {:map, [{key, value}]}: a list and a map of expressions.
  • {:macro_call, name, arguments, keyword_arguments, caller}: what the macro name writes, as safe HTML. caller is nil, or {params, body}, the body that the macro writes for {:caller, arguments}, with its params set from those arguments.

Summary

Types

t()

Which values a language takes as false

Functions

Whether value is true by truth.

Types

t()

@type t() :: %Braces.IR{body: [term()], language: atom(), truth: truth()}

truth()

@type truth() :: :handlebars | :python | :javascript | :php | :liquid | :mustache

Which values a language takes as false:

  • :handlebars: nil, false, 0, "" and [].
  • :python: those and %{}, as Jinja, Django, Tera and Go.
  • :javascript: nil, false, 0 and "", as Nunjucks.
  • :php: nil, false, 0, "", "0", [] and %{}, as Twig.
  • :liquid: nil and false.
  • :mustache: nil, false and [].

Functions

truthy?(value, truth)

Whether value is true by truth.