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

add minijinja templating example

This commit is contained in:
Rob Ede 2022-10-16 21:20:01 +01:00
parent 6953e927ac
commit 5e8883e35a
No known key found for this signature in database
GPG Key ID: 97C636207D3EF933
11 changed files with 686 additions and 364 deletions

857
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -48,6 +48,7 @@ members = [
"templating/askama",
"templating/fluent",
"templating/handlebars",
"templating/minijinja",
"templating/sailfish",
"templating/tera",
"templating/tinytemplate",
@ -61,3 +62,4 @@ members = [
"websockets/echo-actorless",
"websockets/echo",
]
resolver = "2"

View File

@ -0,0 +1,12 @@
[package]
name = "templating-minijinja"
version = "1.0.0"
edition = "2021"
[dependencies]
actix-web = "4"
actix-web-lab = "0.18"
env_logger = "0.9"
log = "0.4"
minijinja = { version = "0.23", features = ["source"] }

View File

@ -0,0 +1,13 @@
# MiniJinja
Minimal example of using the template engine [MiniJinja](https://github.com/Keats/tera) that displays a form.
## Usage
```sh
cd templating/minijinja
cargo run
```
- <http://localhost:8080>
- <http://localhost:8080/non-existing-page> - 404 page rendered using template

View File

@ -0,0 +1,105 @@
use std::collections::HashMap;
use actix_web::{
dev::ServiceResponse,
error,
http::{header::ContentType, StatusCode},
middleware::{ErrorHandlerResponse, ErrorHandlers, Logger},
web, App, Error, HttpResponse, HttpServer, Responder, Result,
};
use actix_web_lab::respond::Html;
/// store tera template in application state
async fn index(
tmpl_env: web::Data<minijinja::Environment<'static>>,
query: web::Query<HashMap<String, String>>,
) -> Result<impl Responder, Error> {
let html = if let Some(name) = query.get("name") {
let tmpl = tmpl_env
.get_template("user.html")
.map_err(|_| error::ErrorInternalServerError("Template error"))?;
let ctx = minijinja::context! {
name,
text => "Welcome!",
};
tmpl.render(ctx)
.map_err(|_| error::ErrorInternalServerError("Template error"))?
} else {
tmpl_env
.get_template("index.html")
.map_err(|_| error::ErrorInternalServerError("Template error"))?
.render(())
.map_err(|_| error::ErrorInternalServerError("Template error"))?
};
Ok(Html(html))
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
log::info!("starting HTTP server at http://localhost:8080");
let mut env: minijinja::Environment<'static> = minijinja::Environment::new();
env.set_source(minijinja::Source::from_path(concat!(
env!("CARGO_MANIFEST_DIR"),
"/templates"
)));
HttpServer::new(move || {
App::new()
.app_data(web::Data::new(env.clone()))
.service(web::resource("/").route(web::get().to(index)))
.wrap(ErrorHandlers::new().handler(StatusCode::NOT_FOUND, not_found))
.wrap(Logger::default())
})
.workers(2)
.bind(("127.0.0.1", 8080))?
.run()
.await
}
/// Error handler for a 404 Page not found error.
fn not_found<B>(svc_res: ServiceResponse<B>) -> Result<ErrorHandlerResponse<B>> {
let res = get_error_response(&svc_res, "Page not found");
Ok(ErrorHandlerResponse::Response(ServiceResponse::new(
svc_res.into_parts().0,
res.map_into_right_body(),
)))
}
/// Generic error handler.
fn get_error_response<B>(res: &ServiceResponse<B>, error: &str) -> HttpResponse {
let req = res.request();
// Provide a fallback to a simple plain text response in case an error occurs during the
// rendering of the error page.
let fallback = |err: &str| {
HttpResponse::build(res.status())
.content_type(ContentType::plaintext())
.body(err.to_string())
};
let ctx = minijinja::context! {
error => error,
status_code => res.status().as_str(),
};
match req
.app_data::<web::Data<minijinja::Environment>>()
.and_then(|tmpl_env| tmpl_env.get_template("error.html").ok())
.and_then(|tmpl| tmpl.render(ctx).ok())
{
Some(body) => Html(body)
.customize()
.with_status(res.status())
.respond_to(&req)
.map_into_boxed_body(),
None => fallback(error),
}
}

View File

@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>{{ error }}</title>
</head>
<body>
<h1>{{ status_code }} {{ error }}</h1>
</body>
</html>

View File

@ -0,0 +1,17 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Actix Web</title>
</head>
<body>
<h1>Welcome!</h1>
<p>
<h3>What is your name?</h3>
<form>
<input type="text" name="name" /><br/>
<p><input type="submit"></p>
</form>
</p>
</body>
</html>

View File

@ -0,0 +1,13 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Actix Web</title>
</head>
<body>
<h1>Hi, {{ name }}!</h1>
<p>
{{ text }}
</p>
</body>
</html>

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]