rework tera instantiation
This commit is contained in:
parent
8ee0f38bc2
commit
b85049f9f7
3 changed files with 14 additions and 2 deletions
16
src/main.rs
16
src/main.rs
|
@ -18,7 +18,7 @@ async fn main() {
|
|||
|
||||
async fn index() -> impl IntoResponse {
|
||||
// Create a new Tera instance and add a template from a string
|
||||
let tera = Tera::new("templates/**/*").unwrap();
|
||||
let tera = tera_create();
|
||||
// Prepare the context with some data
|
||||
let context = tera::Context::new();
|
||||
|
||||
|
@ -29,7 +29,7 @@ async fn index() -> impl IntoResponse {
|
|||
}
|
||||
|
||||
async fn search(Query(params): Query<HashMap<String, String>>) -> impl IntoResponse {
|
||||
let tera = Tera::new("templates/**/*").unwrap();
|
||||
let tera = tera_create();
|
||||
// Prepare the context with some data
|
||||
let mut context = tera::Context::new();
|
||||
context.insert("query", params.get("q").unwrap());
|
||||
|
@ -39,6 +39,18 @@ async fn search(Query(params): Query<HashMap<String, String>>) -> impl IntoRespo
|
|||
header(rendered)
|
||||
}
|
||||
|
||||
fn tera_create() -> Tera {
|
||||
let tera = match Tera::new("templates/**/*.html") {
|
||||
Ok(tera) => tera,
|
||||
Err(error) => {
|
||||
println!("Parsing error(s): {}", error);
|
||||
::std::process::exit(1);
|
||||
}
|
||||
};
|
||||
|
||||
tera
|
||||
}
|
||||
|
||||
fn header(rendered: impl ToString) -> impl IntoResponse {
|
||||
(
|
||||
[(header::CONTENT_TYPE, "text/html; charset=utf-8")],
|
||||
|
|
Loading…
Reference in a new issue