Braces.HubL (Braces v0.1.0)

Copy Markdown View Source

HubSpot's HubL templates, its Jinja for emails and pages.

iex> Braces.HubL.render_to_string("Hi {{ contact.firstname|default('there') }}!", %{"contact" => %{}})
{:ok, "Hi there!"}

Rendering

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

import Braces.HubL, only: :sigils
Braces.render(~HUBL"Hi {{ contact.firstname }}", data)

Braces.HubL.Phoenix.Engine renders .hubl files in Phoenix. Braces.convert/2 converts HubL to and from the other languages.

What renders

What Braces.Jinja renders, and HubL's own:

  • {% unless a %} … {% endunless %}.
  • personalization_token("contact.firstname", "there").
  • Loop filters, macros and {% call %}, inline ifs, lists, dicts and range, as in Braces.Jinja.

Filters HubSpot has and Braces.Filters doesn't, such as format_date, are yours to give as helpers:.

Values are HTML-escaped, except with |safe. HubSpot doesn't escape them: pass escape: false to render as HubSpot does, where |escape still escapes.

{% set %} sets a variable, as in Braces.Jinja. {% extends %}, {% block %} and {% include %} render with the templates given as templates: (see Layouts and includes). Modules and the other tags are a Braces.ConversionError with the tag's line and column.

Summary

Rendering

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

Like compile/2, raising the error.

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

The ~HUBL sigil: HubL 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.HubL, only: :sigils.

Functions

Converts HubL to Handlebars source, or gives a Braces.ConversionError. Takes escape: false to write values without HTML escaping, as HubSpot does.

Like convert/2, raising Braces.ConversionError when the template can't be converted.

Writes a Braces.IR as HubL, or gives a Braces.ConversionError for what HubL can't say.

Reads the template into a Braces.IR, for Braces.convert/2.

Rendering

compile(source, opts \\ [])

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

Compiles HubL 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 HubL source or Braces.Templates, or a function of a name giving {:ok, template} or :error. They're compiled into it.

compile!(source, opts \\ [])

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

Like compile/2, raising the error.

render(template, data, opts \\ [])

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

Renders HubL, 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(template, data, opts \\ [])

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

Like render/3, giving a string.

sigil_HUBL(arg, modifiers)

(macro)

The ~HUBL sigil: HubL 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.HubL, only: :sigils.

Functions

convert(source, opts \\ [])

Converts HubL to Handlebars source, or gives a Braces.ConversionError. Takes escape: false to write values without HTML escaping, as HubSpot does.

convert!(source, opts \\ [])

Like convert/2, raising Braces.ConversionError when the template can't be converted.

from_ir(ir, opts \\ [])

Writes a Braces.IR as HubL, or gives a Braces.ConversionError for what HubL can't say.

to_ir(source, opts \\ [])

Reads the template into a Braces.IR, for Braces.convert/2.