gizlang/jinja
Jinja templates (Jinja2): the language of Hugging Face chat templates and of many configuration and page templates.
import * as Jinja from "gizlang/jinja";Types
Value
type Value = | { kind: "undefined"; } | { kind: "none"; } | { kind: "bool"; value: bool; } | { kind: "int"; value: i64; } | { kind: "float"; value: f64; } | { kind: "str"; value: string; } | { kind: "list"; items: Vec<Value>; } | { kind: "dict"; keys: Vec<string>; values: Vec<Value>; } | { kind: "space"; at: i64; } | { kind: "macro"; at: i64; } | { kind: "method"; name: string; of: Vec<Value>; }A Jinja value. A namespace (space) is shared: set ns.x = ... changes it for every name that holds it. A
method is a value's member read before its call (text.strip, d.items).
undefinednoneboolintfloatstrlistdictspacemacromethod
Template
type Template = { nodes: Vec<Node>; stmts: Vec<Stmt>; body: Vec<i64>; }A parsed template.
nodes:Vec<gizlang_jinja.Node>stmts:Vec<gizlang_jinja.Stmt>body:Vec<i64>
Functions
undefinedValue
function undefinedValue(): ValueJinja's undefined: a name the context does not hold.
none
function none(): ValueJinja's none.
boolean
function boolean(b: bool): ValueThe boolean B.
integer
function integer(i: i64): ValueThe integer I.
str
function str(s: string): ValueThe string S.
list
function list(items: Vec<Value>): ValueA list of ITEMS.
dict
function dict(keys: Vec<string>, values: Vec<Value>): ValueA dict of KEYS to VALUES, in that order.
fromJson
function fromJson(j: JsonValue): ValueA JSON value as Jinja's: objects as dicts, whole numbers as integers.
Effects: fail, alloc.
parse
function parse(source: string, blocks: bool = false): Template throws stringParses source. blocks sets trim_blocks and lstrip_blocks (a block tag takes the newline after it and the
spaces before it on its line), as Hugging Face renders chat templates. The loop controls and the generation block
of chat templates are on.
Effects: fail, read, write, console, io, alloc.
render
function render(t: Template, names: Vec<string>, values: Vec<Value>): string throws stringThe text t writes with the variables names (their values). A raise_exception call throws its message.
Effects: fail, alloc.