mirror of
https://github.com/actix/examples
synced 2024-11-23 22:41:07 +01:00
chore: fix template location
This commit is contained in:
parent
e5fc327af0
commit
c5c208eda7
3
Cargo.lock
generated
3
Cargo.lock
generated
@ -7614,6 +7614,7 @@ version = "1.0.0"
|
||||
dependencies = [
|
||||
"actix-web",
|
||||
"actix-web-lab",
|
||||
"env_logger",
|
||||
"fluent-templates",
|
||||
"handlebars 5.1.0",
|
||||
"serde",
|
||||
@ -7625,6 +7626,8 @@ name = "templating-handlebars"
|
||||
version = "1.0.0"
|
||||
dependencies = [
|
||||
"actix-web",
|
||||
"actix-web-lab",
|
||||
"env_logger",
|
||||
"handlebars 5.1.0",
|
||||
"serde_json",
|
||||
]
|
||||
|
@ -6,7 +6,8 @@ edition = "2021"
|
||||
[dependencies]
|
||||
actix-web.workspace = true
|
||||
actix-web-lab.workspace = true
|
||||
env_logger.workspace = true
|
||||
fluent-templates = { version = "0.9", features = ["handlebars"] }
|
||||
handlebars = { version = "5.1", features = ["dir_source"] }
|
||||
handlebars = { version = "5", features = ["dir_source"] }
|
||||
serde.workspace = true
|
||||
serde_json.workspace = true
|
||||
|
@ -51,6 +51,8 @@ async fn user(
|
||||
|
||||
#[actix_web::main]
|
||||
async fn main() -> io::Result<()> {
|
||||
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
|
||||
|
||||
// Handlebars uses a repository for the compiled templates. This object must be shared between
|
||||
// the application threads, and is therefore passed to the App in an Arc.
|
||||
let mut handlebars = Handlebars::new();
|
||||
@ -58,7 +60,7 @@ async fn main() -> io::Result<()> {
|
||||
// register template dir with Handlebars registry
|
||||
handlebars
|
||||
.register_templates_directory(
|
||||
"./static/templates",
|
||||
"./templates",
|
||||
DirectorySourceOptions {
|
||||
tpl_extension: ".html".to_owned(),
|
||||
hidden: false,
|
||||
@ -79,6 +81,7 @@ async fn main() -> io::Result<()> {
|
||||
.service(index)
|
||||
.service(user)
|
||||
})
|
||||
.workers(2)
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.run()
|
||||
.await
|
||||
|
@ -5,5 +5,7 @@ edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
actix-web.workspace = true
|
||||
handlebars = { version = "5.1", features = ["dir_source"] }
|
||||
actix-web-lab.workspace = true
|
||||
env_logger.workspace = true
|
||||
handlebars = { version = "5", features = ["dir_source"] }
|
||||
serde_json.workspace = true
|
||||
|
@ -6,24 +6,24 @@ use actix_web::{
|
||||
get,
|
||||
http::{header::ContentType, StatusCode},
|
||||
middleware::{ErrorHandlerResponse, ErrorHandlers},
|
||||
web, App, HttpResponse, HttpServer, Result,
|
||||
web, App, HttpResponse, HttpServer, Responder, Result,
|
||||
};
|
||||
use actix_web_lab::respond::Html;
|
||||
use handlebars::{DirectorySourceOptions, Handlebars};
|
||||
use serde_json::json;
|
||||
|
||||
// Macro documentation can be found in the actix_web_codegen crate
|
||||
#[get("/")]
|
||||
async fn index(hb: web::Data<Handlebars<'_>>) -> HttpResponse {
|
||||
async fn index(hb: web::Data<Handlebars<'_>>) -> impl Responder {
|
||||
let data = json!({
|
||||
"name": "Handlebars"
|
||||
});
|
||||
let body = hb.render("index", &data).unwrap();
|
||||
|
||||
HttpResponse::Ok().body(body)
|
||||
Html(body)
|
||||
}
|
||||
|
||||
#[get("/{user}/{data}")]
|
||||
async fn user(hb: web::Data<Handlebars<'_>>, path: web::Path<(String, String)>) -> HttpResponse {
|
||||
async fn user(hb: web::Data<Handlebars<'_>>, path: web::Path<(String, String)>) -> impl Responder {
|
||||
let info = path.into_inner();
|
||||
let data = json!({
|
||||
"user": info.0,
|
||||
@ -31,18 +31,20 @@ async fn user(hb: web::Data<Handlebars<'_>>, path: web::Path<(String, String)>)
|
||||
});
|
||||
let body = hb.render("user", &data).unwrap();
|
||||
|
||||
HttpResponse::Ok().body(body)
|
||||
Html(body)
|
||||
}
|
||||
|
||||
#[actix_web::main]
|
||||
async fn main() -> io::Result<()> {
|
||||
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
|
||||
|
||||
// Handlebars uses a repository for the compiled templates. This object must be
|
||||
// shared between the application threads, and is therefore passed to the
|
||||
// Application Builder as an atomic reference-counted pointer.
|
||||
let mut handlebars = Handlebars::new();
|
||||
handlebars
|
||||
.register_templates_directory(
|
||||
"./static/templates",
|
||||
"./templates",
|
||||
DirectorySourceOptions {
|
||||
tpl_extension: ".html".to_owned(),
|
||||
hidden: false,
|
||||
@ -59,6 +61,7 @@ async fn main() -> io::Result<()> {
|
||||
.service(index)
|
||||
.service(user)
|
||||
})
|
||||
.workers(2)
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.run()
|
||||
.await
|
||||
|
Loading…
Reference in New Issue
Block a user