1
0
mirror of https://github.com/actix/examples synced 2024-11-23 14:31:07 +01:00

Merge pull request #755 from campeis/update_to_handlebars_5.1

update handlebars to version 5.1
This commit is contained in:
Rob Ede 2024-03-05 22:40:36 +00:00 committed by GitHub
commit 95d4b99dbf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 63 additions and 23 deletions

35
Cargo.lock generated
View File

@ -984,7 +984,7 @@ dependencies = [
"fast_chemail",
"fnv",
"futures-util",
"handlebars",
"handlebars 4.5.0",
"http 0.2.9",
"indexmap 2.1.0",
"mime",
@ -3168,24 +3168,24 @@ dependencies = [
[[package]]
name = "fluent-template-macros"
version = "0.8.0"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dec7592cd1f45c1afe9084ce59c62a3a7c266c125c4c2ec97e95b0563c4aa914"
checksum = "0c58fd7421bad2b89506827409317a3088b74d0d637202003f2e87efdc43ae8e"
dependencies = [
"flume 0.10.14",
"ignore",
"once_cell",
"proc-macro2",
"quote",
"syn 1.0.109",
"syn 2.0.48",
"unic-langid",
]
[[package]]
name = "fluent-templates"
version = "0.8.0"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7c3ef2c2152757885365abce32ddf682746062f1b6b3c0824a29fbed6ee4d080"
checksum = "fc023356542b155925aa5e433806ddd33acb46f0218541f869b342676332cd79"
dependencies = [
"arc-swap",
"fluent",
@ -3194,7 +3194,7 @@ dependencies = [
"fluent-syntax",
"fluent-template-macros",
"flume 0.10.14",
"handlebars",
"handlebars 5.1.0",
"heck",
"ignore",
"intl-memoizer",
@ -3678,6 +3678,20 @@ name = "handlebars"
version = "4.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "faa67bab9ff362228eb3d00bd024a4965d8231bbb7921167f0cfa66c6626b225"
dependencies = [
"log",
"pest",
"pest_derive",
"serde",
"serde_json",
"thiserror",
]
[[package]]
name = "handlebars"
version = "5.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ab283476b99e66691dee3f1640fea91487a8d81f50fb5ecc75538f8f8879a1e4"
dependencies = [
"log",
"pest",
@ -7600,8 +7614,9 @@ version = "1.0.0"
dependencies = [
"actix-web",
"actix-web-lab",
"env_logger",
"fluent-templates",
"handlebars",
"handlebars 5.1.0",
"serde",
"serde_json",
]
@ -7611,7 +7626,9 @@ name = "templating-handlebars"
version = "1.0.0"
dependencies = [
"actix-web",
"handlebars",
"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
fluent-templates = { version = "0.8", features = ["handlebars"] }
handlebars = { version = "4.5", features = ["dir_source"] }
env_logger.workspace = true
fluent-templates = { version = "0.9", features = ["handlebars"] }
handlebars = { version = "5", features = ["dir_source"] }
serde.workspace = true
serde_json.workspace = true

View File

@ -10,7 +10,7 @@ use actix_web::{
};
use actix_web_lab::{extract::Path, respond::Html};
use fluent_templates::{static_loader, FluentLoader, Loader as _};
use handlebars::Handlebars;
use handlebars::{DirectorySourceOptions, Handlebars};
use serde_json::json;
mod lang_choice;
@ -51,13 +51,22 @@ 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();
// register template dir with Handlebars registry
handlebars
.register_templates_directory(".html", "./templates")
.register_templates_directory(
"./templates",
DirectorySourceOptions {
tpl_extension: ".html".to_owned(),
hidden: false,
temporary: false,
},
)
.unwrap();
// register Fluent helper with Handlebars registry
@ -72,6 +81,7 @@ async fn main() -> io::Result<()> {
.service(index)
.service(user)
})
.workers(2)
.bind(("127.0.0.1", 8080))?
.run()
.await
@ -85,7 +95,7 @@ fn error_handlers() -> ErrorHandlers<BoxBody> {
// Error handler for a 404 Page not found error.
fn not_found<B>(res: ServiceResponse<B>) -> Result<ErrorHandlerResponse<BoxBody>> {
let lang = LangChoice::from_req(res.request()).lang_id();
let error = LOCALES.lookup(&lang, "error-not-found").unwrap();
let error = LOCALES.lookup(&lang, "error-not-found");
let response = get_error_response(&res, &error);

View File

@ -5,5 +5,7 @@ edition = "2021"
[dependencies]
actix-web.workspace = true
handlebars = { version = "4.5", 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 handlebars::Handlebars;
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,17 +31,26 @@ 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(".html", "./static/templates")
.register_templates_directory(
"./templates",
DirectorySourceOptions {
tpl_extension: ".html".to_owned(),
hidden: false,
temporary: false,
},
)
.unwrap();
let handlebars_ref = web::Data::new(handlebars);
@ -52,6 +61,7 @@ async fn main() -> io::Result<()> {
.service(index)
.service(user)
})
.workers(2)
.bind(("127.0.0.1", 8080))?
.run()
.await