gpt4 book ai didi

Rust & Rocket.rs - 构建布局系统

转载 作者:行者123 更新时间:2023-11-29 08:35:20 30 4
gpt4 key购买 nike

我尝试用 Rocket.rs 创建一个小网站& Handlebars 模板系统。我想创建一个全局布局并将我的内容包含在 <body> 中标签

这是我的代码

templates/
├── layouts
│   └── main.html.hbs
└── pages
   └── home.html.hbs

这是我的布局文件

<!-- templates/layouts/main.html.hbs -->
<!DOCTYPE html>
<html>
<body>
{{~> page}}
</body>
</html>

这是我的内容

<!-- templates/pages/home.html.hbs -->
{{#*inline "page"}}
<h1>hello</h1>
{{/inline}}
{{~> (layouts/main)~}}

还有我的 Rust 代码

// src/main.rs

#[get("/")]
fn home() -> Template {
let map = ();
Template::render("pages/home", &map)
}

fn main() {
rocket::ignite()
.mount("/", routes![home])
.attach(Template::fairing())
.launch();
}

如何将 pages/home 包含到全局布局中?

最佳答案

经过几个小时的搜索,我找到了答案

布局还是不错的

<!-- templates/layouts/main.html.hbs -->
<!DOCTYPE html>
<html>
<body>
{{~> content}}
</body>
</html>

但是对于页面你需要做一些像这样的事情

<!-- templates/pages/home.html.hbs -->
{{#*inline "page"}}
<h1>hello</h1>
{{/inline}}
{{~> layouts/main~}}

关于Rust & Rocket.rs - 构建布局系统,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47436517/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com