1 个不稳定版本
| 0.1.0 | 2024年1月16日 |
|---|
#25 在 #err
34KB
199 行
Leptos Macabre
恐怖简单的服务器端渲染。由 Leptos 提供。
use leptos_macabre::*;
let result: Result<_, &str> = Ok("Done!");
let style = "color: green;";
section! {
h1!{ "Uploads" },
match result {
Ok(msg) => div! {
@htmx-get="/uploads";
@style;
p!{ "Status: ", msg },
},
Err(err) => strong! {
@class="error";
"Uh Oh: " err
},
}
};
语法
- 属性 以
@开头,以;结尾。 - 属性值 是返回
impl leptos::IntoAttribute的表达式。 - 属性值 可以缩写,当名称等于作用域内的变量时。不支持原始标识符(即
r#type)。 - 子元素 是返回
impl leptos::IntoView的表达式。 - 子元素 可以用 可选的 逗号分隔(有助于 rustfmt 和自动缩进,有时)。
细节 & 便利
编译时间 & Lsp
由于一切都是最小递归的 macro_rules!,自动完成和建议非常迅速。
自动 .into_view()
此包中的每个宏都返回一个 View,这有意偏离了 Leptos,以避免在每个分支上都需要 .into_view()(一个可理解的要求)。
use leptos_macabre::*;
let result = Some(true);
let href = "#section";
match result {
Some(true) => a!(@href; "Go here!"),
Some(false) => p!(@class="bad"; "Uh-oh!"),
None => div!(),
};
每个元素都是一个宏。
每个元素都是一个独立的宏。这可以节省许多按键。它不同于大多数 Rust HTML 宏。
use leptos::*;
use leptos_macabre::*;
view! { <button>"27 keystrokes"</button> }
button! { "10 keystrokes" }
重新导出
此包导出所需的工作的 leptos_dom 项。如果您只想在服务器上制作 HTML,则可以跳过添加 leptos 作为其自己的依赖项。
缺点
没有可以用 script! 或正常 HTML 事件处理程序实现的反应性。没有信号。性能可能比直接与 leptos::view! 相比更差。
仅对带属性(props)的 leptos 组件提供基本支持。生成的 Prop* 结构体需要作为参数提供,这意味着您将无法使用许多原本通过 #[component] (可选属性等)提供的便利。
use leptos_macabre::*;
use leptos::component;
#[component]
fn MyButton() -> impl leptos::IntoView {}
#[component]
fn MyA(href: &'static str) -> impl leptos::IntoView {}
p!{ MyButton };
p!{ MyButton() };
p!{ MyA(MyAProps { href: "#red" }) };
由于子元素只是返回 impl IntoView 表达式,您可以使用老式的函数来替换组件。
use leptos_macabre::*;
fn my_button() -> impl leptos::IntoView { }
fn my_a(href: &'static str) -> impl leptos::IntoView {}
fn my_b(inner: Option<bool>) -> impl leptos::IntoView {}
div! {
my_button,
my_button(),
my_a("#red"),
my_b(Some(true)) ,
};
名称
我一直觉得 horrorshow crate 很有趣,而 leptos 则与疾病 莱姆病 很接近。这全部都显得有些凄凉。其次,这个 crate 是使用嵌套的 macro_rules! 创建的,这真的是令人毛骨悚然。
依赖关系
约 14-27MB
约 408K SLoC