| ▲ | slopinthebag 5 hours 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" }
}
}
}
}
|
|
| ▲ | dnautics 4 hours ago | parent | next [-] |
| > 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. |
| |
| ▲ | josephg 4 hours ago | parent | next [-] | | 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>
}
}
| |
| ▲ | slopinthebag 3 hours ago | parent | prev [-] | | i prefer it, but you can also get html if you want eg html! {
<div onclick=|_ev: web_sys::MouseEvent| {}>
You can type text right into the elements
{ component }
{ text_var }
</div>
};
|
|
|
| ▲ | asimovDev 4 hours ago | parent | prev | next [-] |
| This is just Twig templates, crazy. It's cool that Rust can do this out of a box |
| |
| ▲ | slopinthebag 4 hours ago | parent [-] | | 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 |
|
|
| ▲ | skavi 3 hours ago | parent | prev | next [-] |
| but you have invented syntax |
|
| ▲ | sroerick 4 hours ago | parent | prev | next [-] |
| That is super cool actually |
|
| ▲ | eggsandbeer 4 hours ago | parent | prev [-] |
| [dead] |