Templating reference

niji has builtin support for templating using its niji.Template API.

The templating language used for this is niji's own dialect of mustache. You can look at the mustache documentation for general information on how it works, it mostly applies to niji's dialect as well.

One major difference from the mustache specification is that triple mustaches ({{{name}}}), which are normally used to disable escaping of HTML characters, are not supported. Instead, niji templates just never escape HTML characters.

Custom Formats

The one extension that niji makes to the base mustache specification is custom formats for complex types. niji's mustache dialect provides special syntax for displaying formattable types using format strings. This feature is necessary because the vast array of targets niji supports may expect vastly different formats for different data types, and it is impractial to split those datatypes up into more atomic parts.

For example, you can render a color using a custom format like this:

{{my_color : "🔴{r}🟢{g}🔵{b}"}}

The result of rendering this with my_color = "#abcdefff" would be 🔴171🟢205🔵239.

Often times, you will want to use a specific format for the entirety of a template. You can do this by adding a format specification for the type name at the top of your file like this:

{{% "color" : "rgba({r}, {g}, {b}, {a})" %}}

.some-class {
    background-color: {{my_color}}
}

The general format for format strings is exactly the same as that which is used by Rust's standard library, and you can insert any of the properties defined for the type you're working with. A list can be found in the following section.

Formattable Types

For now, the only formattable type is niji.Color. It exposes the following properties:

NameDescription
rThe red component as an integer between 0 and 255
gThe green component as an integer between 0 and 255
bThe blue component as an integer between 0 and 255
aThe alpha component as an integer between 0 and 255
rxThe red component as two hexadecimal digits
gxThe green component as two hexadecimal digits
bxThe blue component as two hexadecimal digits
axThe alpha component as two hexadecimal digits
rfThe red component as a float between 0 and 1
gfThe green component as a float between 0 and 1
bfThe blue component as a float between 0 and 1
afThe alpha component as a float between 0 and 1