1 个不稳定版本
新 0.1.1 | 2024年8月15日 |
---|
#444 在 HTTP服务器
112 每月下载量
42KB
1K SLoC
Unchained
模板渲染器和路由器
在复活节假期的晚上创建
- 易于使用
- 易于扩展
- 无额外依赖
示例
fn main() {
let mut context = HashMap::new();
context.insert("page_links".to_string(), ctx_vec(vec![
ctx_map([("href", ctx_str("/#about")), ("label", ctx_str("About me"))]),
ctx_map([("href", ctx_str("/experience")), ("label", ctx_str("Experience"))]),
ctx_map([("href", ctx_str("/skills")), ("label", ctx_str("Skills"))]),
]));
let template = template("templates/landing-page.html", Some(context));
let routes = vec![
Route::new(GET, "/", ResponseContent::Str(
match &template {
Ok(template) => template.to_string(),
Err(_e) => panic!("Could not render template"),
}
)),
Route::new(GET, "/images/*", ResponseContent::FolderAccess)
];
start_server(routes, ServerOptions { address: Some("localhost:8080".to_string()) });
}
<!-- templates/landing-page.html -->
{* component templates/base.html {
<div class="grid grid-cols-2 gap-4 m-5">
{* for button in page_links {
{* component templates/front-button.html label=button.label link=button.href *}
} *}
</div>
<div class="w-full mt-96 sm-p-10 p-4">
<h1 id="about" class="text-4xl font-bold">About me</h1>
<div class="p-10 flex flex-row gap-3 overflow-x-scroll">
{* for image in images {
<img alt="{* image.alt *}" src="/images/{* image.path *}" width="500" height="500">
} *}
</div>
</div>
} *}
语法
op_name: "for" | "if" | "component" | "slot" ...
parameters: (\w+\s*)+
html: !( template_operation )
children: "{" html "}"
template_operation: "{*" op_name parameters? children? "*}"