1
0
mirror of https://github.com/actix/examples synced 2025-06-26 17:17:42 +02:00

update handlebars to version 5.1

This commit is contained in:
Alessandro Campeis
2024-03-04 13:05:13 +01:00
parent 2f7a1db615
commit e5fc327af0
5 changed files with 46 additions and 18 deletions

View File

@ -6,7 +6,7 @@ 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"] }
fluent-templates = { version = "0.9", features = ["handlebars"] }
handlebars = { version = "5.1", 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;
@ -57,7 +57,14 @@ async fn main() -> io::Result<()> {
// register template dir with Handlebars registry
handlebars
.register_templates_directory(".html", "./templates")
.register_templates_directory(
"./static/templates",
DirectorySourceOptions {
tpl_extension: ".html".to_owned(),
hidden: false,
temporary: false,
},
)
.unwrap();
// register Fluent helper with Handlebars registry
@ -85,7 +92,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,5 @@ edition = "2021"
[dependencies]
actix-web.workspace = true
handlebars = { version = "4.5", features = ["dir_source"] }
handlebars = { version = "5.1", features = ["dir_source"] }
serde_json.workspace = true

View File

@ -8,7 +8,7 @@ use actix_web::{
middleware::{ErrorHandlerResponse, ErrorHandlers},
web, App, HttpResponse, HttpServer, Result,
};
use handlebars::Handlebars;
use handlebars::{DirectorySourceOptions, Handlebars};
use serde_json::json;
// Macro documentation can be found in the actix_web_codegen crate
@ -41,7 +41,14 @@ async fn main() -> io::Result<()> {
// Application Builder as an atomic reference-counted pointer.
let mut handlebars = Handlebars::new();
handlebars
.register_templates_directory(".html", "./static/templates")
.register_templates_directory(
"./static/templates",
DirectorySourceOptions {
tpl_extension: ".html".to_owned(),
hidden: false,
temporary: false,
},
)
.unwrap();
let handlebars_ref = web::Data::new(handlebars);