r/webdev • u/Rechenplaner • Oct 31 '26
Showoff Saturday Factories.ts: An efficient JS DSL for building HTML, SVG, and MathML directly in backend code, without a template engine.
https://github.com/ts-series/factories
Build HTML extremely comfortably in the backend using plain JavaScript. The advantage is that there is no separate template language or build process, but rather a complete fusion with JavaScript for maximum flexibility. An example:
import {
a, body, br, button, h1, head, header, html, input, label, li, main, nav, p, script, title, ul
} from "@ts-series/factories";
type Item = { name: string, href: string, current: boolean };
/** Builds the navigation list, flagging the current entry for assistive technology. */
function navigation(items: Item[]) {
return nav({ ariaLabel: "Main" },
ul(items.map(item => li(
a(item.href, item.name).set(item.current ? { ariaCurrent: "page" } : null)
)))
);
}
/** Builds a complete page for the given user and navigation entries. */
function page(user: string | null, items: Item[], values: number[]) {
return html({ lang: "en" },
head(title("Demo")),
body(
header(navigation(items)),
main(
h1("Hello, ", user ?? "stranger", "!"),
p("First line", br, "Second line"),
user === null
? p("Please sign in.")
: p("Welcome back."),
label("Search").for("q"),
input.text({ id: "q", name: "q" }).placeholder("Type here…").required(),
button.submit("Send").class("primary", user === null && "disabled").set("disabled")
),
script.module().importOnly("/chart.js", "render", { values, title: "Values" })
)
);
}
const items: Item[] = [
{ name: "Home", href: "/", current: true },
{ name: "About", href: "/about", current: false }
];
const output = page("<Guest>", items, [1, 2, 3]).expand(4);
0
Upvotes
3
u/ConfectionPlastic625 Oct 31 '26
Have you tried this with dynamic content yet, like generating a whole page from an API response?
2
u/patryk_builds Oct 31 '26
Nice that the example passes Guest as the user, i assume that's there to show escaping is on by default? that's the first thing i'd check with any html-in-code approach, since one forgotten escape is an XSS.Also like that `user === null && "disabled"` just works inside .class(), the falsy values get dropped instead of rendering "false". small thing but it's what makes these DSLs pleasant vs. string concatenation.Python has a few similar ones (htpy, dominate), always interesting to see how people solve it in TS with types on top
3
u/pampuliopampam Oct 31 '26
So... after building this thing, and writing this post, did you take a moment to reflect that you've just recreated jsx... but worse?
like... you've muxed the idea of children and props passing, so actually building anything is just far more complicated than using the industry standard
did this make you think about the thing you've made? I can't tell if you're even aware of anything because of reddit's baffling decision to use hidden history, and your github profile being utterly lackluster. The only thing that differentiates you from a paid shill for one of the LLMs that wrote all this is that you've not leaked the attribution to the model in your code.