1
0
mirror of https://github.com/actix/examples synced 2024-11-27 16:02:57 +01:00

chore: fix template location

This commit is contained in:
Rob Ede 2024-03-05 22:30:51 +00:00
parent e5fc327af0
commit c5c208eda7
No known key found for this signature in database
GPG Key ID: 97C636207D3EF933
8 changed files with 22 additions and 10 deletions

3
Cargo.lock generated
View File

@ -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",
]

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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