Braces.Filters (Braces v0.1.0)

Copy Markdown View Source

The filters, tests and operators of Jinja, Django, Nunjucks, Twig, Tera, Mailchimp, HubL and Klaviyo, which those languages' modules render with. They're Handlebars helpers, so a template converted to Handlebars renders with them too:

{:ok, source} = Braces.Django.convert("Hi {{ first_name|default:'friend'|title }}")
source
#=> "Hi {{title (default first_name \"friend\")}}"

Braces.Handlebars.render_to_string(Braces.Handlebars.compile!(source), %{}, helpers: Braces.Filters)
#=> {:ok, "Hi Friend"}

Add your own helpers after them, helpers: [Braces.Filters, MyApp.EmailHelpers], for filters that aren't here: a filter is converted to a helper of the same name.

Values are true unless they're nil, false, 0, "", [] or %{}, as in Python. Comparisons compare numbers as numbers, and a number with a string that is a number, so "18" equals 18, as Mailchimp's merge fields and Klaviyo's == 1 expect. Other values are compared as strings.

Helpers

  • Comparisons: eq, ne, gt, gte, lt, lte, in (a list's element, a map's key, or a substring), and, or, not, and is for HubL's tests (defined, undefined, none, truthy, even, odd, number, string, sequence, mapping, divisibleby, string_containing, string_startingwith, string_endingwith, empty, iterable, containing, boolean, true, false, integer, float, lower, upper, escaped, filter, test).
  • Text: upper (upcase), lower (downcase), title, capitalize (capfirst), trim (with chars), truncate, truncate_after, replace (find_replace), striptags (strip_html), urlencode, escape (e, escape_html), safe, append, prepend, concat, split, string, and Jinja's wordcount, wordwrap, indent, center, format, filesizeformat, urlize and xmlattr.
  • Lists: length (size, count), join, first, last, reverse, and Jinja's sort, list, unique, sum, min, max, batch, slice, map, select, reject, selectattr, rejectattr, groupby, dictsort and items, with Jinja's keyword arguments as the hash: {{sort people attribute="name"}}. groupby, dictsort and items give {key, value} tuples.
  • Numbers in words: ordinal (3rd), number_words (three) and ordinal_words (third), for numbers from 0 to 99.
  • Numbers: add (plus), subtract (minus), multiply, divide, modulo, round, abs, int (with default and base), float (with default), floatformat.
  • default: the value, or the fallback when the value isn't true. With undefined: true, as Jinja's default without its second argument, only when it's missing; with empty: true, as Twig's, when it's missing, false, "", [] or %{}; with truth:, when it's false by that language's rules, as truthy.
  • truthy: whether a value is true, by Python's rules or a language's, (truthy x "liquid").
  • Dates: date formats a date with PHP's and Django's format characters, as *|DATE:d/m/y|* and Klaviyo's |date:'F j, Y'; strftime formats one with %Y-%m-%d; now is the current time in UTC; and format_date_string reads an ISO 8601 string.

Summary

Functions

The items in lists of count, the last filled with fill when it's given.

The text in the middle of width characters, as Python's center.

A map's {key, value} pairs sorted by key, or by: "value", as Jinja's dictsort.

An empty list, for a template with no literal for one.

An empty map, for a template with no literal for one.

A number of bytes as text, as Jinja's filesizeformat: 3.0 kB, or with binary, 2.9 KiB.

a // b: the quotient rounded down, as Python's.

The text with %s, %d, %i, %f and %r replaced by the arguments, as Python's % formats them.

The items in groups by their attribute, as Jinja's groupby: {grouper, items} pairs in the groupers' order, read as group.grouper and group.list, or in a loop as for grouper, items in …; default for an item without it, and case_sensitive.

Each line after the first indented by width spaces, or by width when it's text, as Jinja's indent: first indents the first line too, and blank blank lines.

A map's {key, value} pairs, as Jinja's items.

A map's keys, or a list's indexes, as Twig's keys gives them.

A Liquid loop's limit:: the list's first count items.

A Liquid loop's offset:: the list after its first count items.

Liquid's range, (1..5): the integers from one to the other, both included.

A list of the value's items: a map's keys, or a string's characters.

Each item through a filter, map("upper"), or its attribute, map(attribute="name"), with default for an item without it.

The largest item, as Jinja's max: case_sensitive, attribute.

The smallest item, as Jinja's min: case_sensitive, attribute.

a ** b: a to the power b.

The items a test is false for, reject("odd"), or that are false.

The items whose attribute a test is false for, or is false.

The items a test is true for, select("odd"), or that are true.

The items whose attribute a test is true for, or is true.

The items in count lists, as Jinja's slice, the shorter filled with fill.

A list sorted, as Jinja's sort: reverse, case_sensitive (false: "a" and "B" sort as "a" and "b") and attribute, a key, a path, "a.b", or keys, "a,b", to sort items by. Given in order or by name: {{sort xs true}}, {{sort xs attribute="name"}}. A map sorts its keys.

Formats a date with strftime's format, %Y-%m-%d, as Tera's, Liquid's and Python's dates do.

The value as text.

The sum of the items, or of their attribute, from start.

The text without spaces, or the characters of chars, at its ends.

The first length characters, then ending when there were more, as Tera's truncate.

Whether a value is true by a language's rules, "python" when not given: see Braces.IR.truth/0.

The items without repeats, the first of each kept, as Jinja's unique: case_sensitive, attribute.

The text with its links, http://, https://, www. and domains, and email addresses as <a> links, escaped, as Jinja's urlize: trim_url_limit, rel (noopener), target, nofollow and extra_schemes.

How many words: runs of letters, digits and _.

The text in lines of at most width characters, as Jinja's wordwrap: each line of the text wrapped on its own, joined with wrapstring, and words longer than a line broken unless break_long_words is false.

A map as HTML attributes, as Jinja's xmlattr: key="value" for each key whose value isn't missing or nil, escaped.

Functions

abs(value, options)

add(a, b, options)

and(a, b, options)

and(a, b, c, options)

append(value, suffix, options)

batch(value, count, options)

The items in lists of count, the last filled with fill when it's given.

batch(value, count, fill, options)

capfirst(value, options)

capitalize(value, options)

center(value, options)

The text in the middle of width characters, as Python's center.

center(value, width, options)

concat(a, b, options)

count(value, options)

date(value, format, options)

default(value, fallback, options)

dictsort(value, options)

A map's {key, value} pairs sorted by key, or by: "value", as Jinja's dictsort.

divide(a, b, options)

downcase(value, options)

e(value, options)

empty_list(options)

An empty list, for a template with no literal for one.

empty_map(options)

An empty map, for a template with no literal for one.

eq(a, b, options)

escape(value, options)

escape_html(value, options)

filesizeformat(value, options)

A number of bytes as text, as Jinja's filesizeformat: 3.0 kB, or with binary, 2.9 KiB.

filesizeformat(value, binary, options)

find_replace(value, both, options)

first(value, options)

float(value, options)

float(value, default, options)

floatformat(value, options)

floatformat(value, places, options)

floordiv(a, b, options)

a // b: the quotient rounded down, as Python's.

format(value, options)

The text with %s, %d, %i, %f and %r replaced by the arguments, as Python's % formats them.

format(value, a, options)

format(value, a, b, options)

format(value, a, b, c, options)

format(value, a, b, c, d, options)

format_date_string(value, options)

groupby(value, attribute, options)

The items in groups by their attribute, as Jinja's groupby: {grouper, items} pairs in the groupers' order, read as group.grouper and group.list, or in a loop as for grouper, items in …; default for an item without it, and case_sensitive.

gt(a, b, options)

gte(a, b, options)

in(item, container, options)

indent(value, options)

indent(value, width, options)

Each line after the first indented by width spaces, or by width when it's text, as Jinja's indent: first indents the first line too, and blank blank lines.

indent(value, width, first, options)

indent(value, width, first, blank, options)

int(value, options)

int(value, default, options)

int(value, default, base, options)

is(value, test, options)

is(value, test, argument, options)

items(value, options)

A map's {key, value} pairs, as Jinja's items.

join(value, options)

join(value, separator, options)

keys(map, options)

A map's keys, or a list's indexes, as Twig's keys gives them.

last(value, options)

length(value, options)

liquid_limit(list, count, options)

A Liquid loop's limit:: the list's first count items.

liquid_limit(list, count, offset, options)

liquid_offset(list, count, options)

A Liquid loop's offset:: the list after its first count items.

liquid_range(from, to, options)

Liquid's range, (1..5): the integers from one to the other, both included.

list(value, options)

A list of the value's items: a map's keys, or a string's characters.

lower(value, options)

lt(a, b, options)

lte(a, b, options)

map(value, options)

Each item through a filter, map("upper"), or its attribute, map(attribute="name"), with default for an item without it.

map(value, filter, options)

map(value, filter, a, options)

map(value, filter, a, b, options)

max(value, options)

The largest item, as Jinja's max: case_sensitive, attribute.

min(value, options)

The smallest item, as Jinja's min: case_sensitive, attribute.

minus(a, b, options)

modulo(a, b, options)

multiply(a, b, options)

ne(a, b, options)

not(value, options)

now(options)

number_words(value, options)

or(a, b, options)

or(a, b, c, options)

ordinal(value, options)

ordinal_words(value, options)

plus(a, b, options)

power(a, b, options)

a ** b: a to the power b.

prepend(value, prefix, options)

reject(value, options)

The items a test is false for, reject("odd"), or that are false.

reject(value, test, options)

reject(value, test, a, options)

rejectattr(value, attribute, options)

The items whose attribute a test is false for, or is false.

rejectattr(value, attribute, test, options)

rejectattr(value, attribute, test, a, options)

replace(value, both, options)

replace(value, from, to, options)

reverse(value, options)

round(value, options)

round(value, precision, options)

safe(value, options)

select(value, options)

The items a test is true for, select("odd"), or that are true.

select(value, test, options)

select(value, test, a, options)

selectattr(value, attribute, options)

The items whose attribute a test is true for, or is true.

selectattr(value, attribute, test, options)

selectattr(value, attribute, test, a, options)

size(value, options)

slice(value, count, options)

The items in count lists, as Jinja's slice, the shorter filled with fill.

slice(value, count, fill, options)

sort(value, options)

A list sorted, as Jinja's sort: reverse, case_sensitive (false: "a" and "B" sort as "a" and "b") and attribute, a key, a path, "a.b", or keys, "a,b", to sort items by. Given in order or by name: {{sort xs true}}, {{sort xs attribute="name"}}. A map sorts its keys.

sort(value, reverse, options)

sort(value, reverse, case_sensitive, options)

sort(value, reverse, case_sensitive, attribute, options)

split(value, separator, options)

strftime(value, format, options)

Formats a date with strftime's format, %Y-%m-%d, as Tera's, Liquid's and Python's dates do.

string(value, options)

The value as text.

strip_html(value, options)

striptags(value, options)

subtract(a, b, options)

sum(value, options)

The sum of the items, or of their attribute, from start.

sum(value, attribute, options)

title(value, options)

trim(value, options)

trim(value, chars, options)

The text without spaces, or the characters of chars, at its ends.

truncate(value, options)

truncate(value, length, options)

truncate_after(value, length, options)

The first length characters, then ending when there were more, as Tera's truncate.

truncate_after(value, length, ending, options)

truthy(value, options)

Whether a value is true by a language's rules, "python" when not given: see Braces.IR.truth/0.

truthy(value, truth, options)

unique(value, options)

The items without repeats, the first of each kept, as Jinja's unique: case_sensitive, attribute.

upcase(value, options)

upper(value, options)

urlencode(value, options)

urlize(value, options)

urlize(value, trim_url_limit, options)

The text with its links, http://, https://, www. and domains, and email addresses as <a> links, escaped, as Jinja's urlize: trim_url_limit, rel (noopener), target, nofollow and extra_schemes.

wordcount(value, options)

How many words: runs of letters, digits and _.

wordwrap(value, options)

wordwrap(value, width, options)

The text in lines of at most width characters, as Jinja's wordwrap: each line of the text wrapped on its own, joined with wrapstring, and words longer than a line broken unless break_long_words is false.

wordwrap(value, width, break_long_words, options)

xmlattr(value, options)

A map as HTML attributes, as Jinja's xmlattr: key="value" for each key whose value isn't missing or nil, escaped.

xmlattr(value, autospace, options)