Type-safe HTML templating in Zig
lalinsky.com42 points by lukaslalinsky 2 hours ago
42 points by lukaslalinsky 2 hours ago
I love the idea! I could imagine something similar created for other languages too. Perhaps written in a way that a template could be reused in, let's say Django, and a JS React frontend. With all the type-safety that tsx provides.
I always thought that the type-safe XML/HTML in Ur/Web was elegant.
For examples, see http://www.impredicative.com/ur/demo/
W3C: Why can't we just have <includes src="./header.html"> and be done with it?
You might be interested in https://github.com/WICG/declarative-partial-updates
<?php include "header.html" ?>
Yes, but then you need php
Why is there no direct support for templating in HTML itself?
<!-- #include file="header.html" -->
https://en.wikipedia.org/wiki/Server_Side_IncludesSSIs are not widely supported any more, and were never a part of the HTML standard
(but yes- I used these a lot like 25 years ago)
one thing i love about rust's macros is that you can accomplish this without needing a separate file type or inventing syntax
#[component]
pub fn HomePage(user: Option<User>) -> Element {
rsx! {
Layout {
title: "Home",
if let Some(u) = user {
h1 { "Welcome back, {u.name}!" }
a { href: "/users/{u.id}", "Your profile" }
} else {
h1 { "Welcome, guest!" }
a { href: "/login", "Log in" }
}
}
}
}This is just Twig templates, crazy. It's cool that Rust can do this out of a box
yep and you get rust's lsp support too, autoformatting, etc.
this is rsx, but there are other macros that look just like html. i prefer this though, it's much more compact and readable imo
> you can accomplish this
That's not HTML though, you have to learn a whole new dsl that must conform to rust parser rules. So, not the same thing at all.
You can do actual html. This is Leptos, a Solidjs like rust library:
#[component]
fn App() -> impl IntoView {
let (count, set_count) = signal(0);
view! {
<button
on:click=move |_| set_count.set(3) >
"Click me”</button>
<p>{move || count.get() * 2}</p>
}
}[flagged]