GizStack
View .mdOpen in ChatGPTOpen in Claude

gizlang/jinja

Jinja templates (Jinja2): the language of Hugging Face chat templates and of many configuration and page templates.

gizlang
import * as Jinja from "gizlang/jinja";

Types

Value

gizlang
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).

  • undefined
  • none
  • bool
  • int
  • float
  • str
  • list
  • dict
  • space
  • macro
  • method

Template

gizlang
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

gizlang
function undefinedValue(): Value

Jinja's undefined: a name the context does not hold.

none

gizlang
function none(): Value

Jinja's none.

boolean

gizlang
function boolean(b: bool): Value

The boolean B.

integer

gizlang
function integer(i: i64): Value

The integer I.

str

gizlang
function str(s: string): Value

The string S.

list

gizlang
function list(items: Vec<Value>): Value

A list of ITEMS.

dict

gizlang
function dict(keys: Vec<string>, values: Vec<Value>): Value

A dict of KEYS to VALUES, in that order.

fromJson

gizlang
function fromJson(j: JsonValue): Value

A JSON value as Jinja's: objects as dicts, whole numbers as integers.

Effects: fail, alloc.

parse

gizlang
function parse(source: string, blocks: bool = false): Template throws string

Parses 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

gizlang
function render(t: Template, names: Vec<string>, values: Vec<Value>): string throws string

The text t writes with the variables names (their values). A raise_exception call throws its message.

Effects: fail, alloc.