mirror of
https://github.com/actix/examples
synced 2025-06-28 18:00:37 +02:00
add askama example
This commit is contained in:
14
template_askama/Cargo.toml
Normal file
14
template_askama/Cargo.toml
Normal file
@ -0,0 +1,14 @@
|
||||
[package]
|
||||
name = "template-askama"
|
||||
version = "0.1.0"
|
||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||
workspace = "../"
|
||||
|
||||
[dependencies]
|
||||
env_logger = "0.5"
|
||||
actix = "0.5"
|
||||
actix-web = "^0.5"
|
||||
askama = "0.6"
|
||||
|
||||
[build-dependencies]
|
||||
askama = "0.6"
|
5
template_askama/build.rs
Normal file
5
template_askama/build.rs
Normal file
@ -0,0 +1,5 @@
|
||||
extern crate askama;
|
||||
|
||||
fn main() {
|
||||
askama::rerun_if_templates_changed();
|
||||
}
|
45
template_askama/src/main.rs
Normal file
45
template_askama/src/main.rs
Normal file
@ -0,0 +1,45 @@
|
||||
extern crate actix;
|
||||
extern crate actix_web;
|
||||
#[macro_use]
|
||||
extern crate askama;
|
||||
|
||||
use actix::prelude::*;
|
||||
use actix_web::{http, server, App, HttpRequest, HttpResponse, Result};
|
||||
use askama::Template;
|
||||
|
||||
#[derive(Template)]
|
||||
#[template(path = "user.html")]
|
||||
struct UserTemplate<'a> {
|
||||
name: &'a str,
|
||||
text: &'a str,
|
||||
}
|
||||
|
||||
#[derive(Template)]
|
||||
#[template(path = "index.html")]
|
||||
struct Index;
|
||||
|
||||
fn index(req: HttpRequest) -> Result<HttpResponse> {
|
||||
let s = if let Some(name) = req.query().get("name") {
|
||||
UserTemplate {
|
||||
name: name,
|
||||
text: "Welcome!",
|
||||
}.render()
|
||||
.unwrap()
|
||||
} else {
|
||||
Index.render().unwrap()
|
||||
};
|
||||
Ok(HttpResponse::Ok().content_type("text/html").body(s))
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let sys = System::new("template-askama");
|
||||
|
||||
// start http server
|
||||
server::new(move || App::new().resource("/", |r| r.method(http::Method::GET).f(index)))
|
||||
.bind("0.0.0.0:8080")
|
||||
.unwrap()
|
||||
.start();
|
||||
|
||||
println!("Started http server: 127.0.0.1:8080");
|
||||
let _ = sys.run();
|
||||
}
|
17
template_askama/templates/index.html
Normal file
17
template_askama/templates/index.html
Normal 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>
|
13
template_askama/templates/user.html
Normal file
13
template_askama/templates/user.html
Normal 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>
|
Reference in New Issue
Block a user