Declaration
Template :: struct {
// Starts recording one link; used by generated templates.
// ```jai
// out: Out;
// defer deinit(*out);
// Template.begin(*out);
// Template.piece(*out, "/");
// Template.finish(*out);
// assert(out.links[0] == "/");
// ```
begin :: (out: *Out) {
out.link_start = out.link.count;
}
// Records unescaped link bytes when recording is enabled.
// ```jai
// out: Out;
// defer deinit(*out);
// Template.begin(*out);
// Template.piece(*out, "/");
// Template.finish(*out);
// assert(out.links[0] == "/");
// ```
piece :: (out: *Out, s: string) {
if !out.record_links return;
before := out.link.data;
array_add(*out.link, ..(cast([] u8) s));
if before != out.link.data {
for offset, i: out.offsets out.links[i] = string.{ offset.count, out.link.data + offset.start };
}
}
// Saves an offset into the page's link bytes; earlier links survive growth.
// ```jai
// out: Out;
// defer deinit(*out);
// Template.begin(*out);
// Template.piece(*out, "/");
// Template.finish(*out);
// assert(out.links[0] == "/");
// ```
finish :: (out: *Out) {
if !out.record_links return;
count := out.link.count - out.link_start;
array_add(*out.offsets, .{ out.link_start, count });
array_add(*out.links, string.{ count, out.link.data + out.link_start });
}
// Appends a typed template hole; unsupported types report at location.
// ```jai
// out: Out;
// defer deinit(*out);
// Template.value(*out, "<&", false, false, false, "text");
// assert(to_string(*out) == "<&");
// ```
value :: (out: *Out, v: $T, $attribute: bool, $raw: bool, $link: bool, $hole: string, $location: Source_Code_Location = #caller_location) {
#if T == Html {
append_html(out, v);
#if link piece(out, cast(string) v);
} else #if raw {
#run compiler_report(tprint("Web: hole `{%}` has type %; script/style holes require Html", hole, T), location);
} else #if T == string {
#if attribute escape_attribute(out, v);
else escape_text(out, v);
#if link piece(out, v);
} else #if type_info(T).type == .INTEGER {
text := tprint("%", v);
append(out, text);
#if link piece(out, text);
} else {
#run compiler_report(tprint("Web: hole `{%}` has unsupported type %", hole, T), location);
}
}
}Implementation entry points used by generated templates.
out: Out;
defer deinit(*out);
Template.value(*out, 12, false, false, false, "12");
assert(to_string(*out) == "12");