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

add minijinja templating example

This commit is contained in:
Rob Ede
2022-10-16 21:20:01 +01:00
parent 6953e927ac
commit 5e8883e35a
11 changed files with 686 additions and 364 deletions

View File

@ -4,6 +4,8 @@ version = "1.0.0"
edition = "2021"
[dependencies]
env_logger = "0.9.0"
tera = "1.8.0"
actix-web = "4"
actix-web-lab = "0.18"
env_logger = "0.9"
tera = "1.8.0"

View File

@ -4,15 +4,10 @@ Minimal example of using the template [tera](https://github.com/Keats/tera) that
## Usage
### server
```sh
cd templating/tera
cargo run (or ``cargo watch -x run``)
# Started http server: 127.0.0.1:8080
cargo run
```
### web client
- [http://localhost:8080](http://localhost:8080)
- [http://localhost:8080/non-existing-page](http://localhost:8080/non-existing-page) - 404 page rendered using template
- <http://localhost:8080>
- <http://localhost:8080/non-existing-page> - 404 page rendered using template

View File

@ -8,6 +8,7 @@ use actix_web::{
middleware::{self, ErrorHandlerResponse, ErrorHandlers},
web, App, Error, HttpResponse, HttpServer, Result,
};
use actix_web_lab::respond::Html;
use tera::Tera;
// store tera template in application state
@ -26,7 +27,8 @@ async fn index(
tmpl.render("index.html", &tera::Context::new())
.map_err(|_| error::ErrorInternalServerError("Template error"))?
};
Ok(HttpResponse::Ok().content_type("text/html").body(s))
Ok(Html(s))
}
#[actix_web::main]