mirror of
https://github.com/fafhrd91/actix-web
synced 2024-11-24 00:21:08 +01:00
add tera example
This commit is contained in:
parent
26af6040ff
commit
3e8a6c3988
@ -56,7 +56,7 @@ fn with_param(req: HttpRequest) -> Result<HttpResponse>
|
|||||||
fn main() {
|
fn main() {
|
||||||
::std::env::set_var("RUST_LOG", "actix_web=info");
|
::std::env::set_var("RUST_LOG", "actix_web=info");
|
||||||
let _ = env_logger::init();
|
let _ = env_logger::init();
|
||||||
let sys = actix::System::new("ws-example");
|
let sys = actix::System::new("basic-example");
|
||||||
|
|
||||||
HttpServer::new(
|
HttpServer::new(
|
||||||
|| Application::new()
|
|| Application::new()
|
||||||
|
10
examples/template_tera/Cargo.toml
Normal file
10
examples/template_tera/Cargo.toml
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
[package]
|
||||||
|
name = "template-tera"
|
||||||
|
version = "0.1.0"
|
||||||
|
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
env_logger = "0.4"
|
||||||
|
actix = "^0.3.1"
|
||||||
|
actix-web = { git = "https://github.com/actix/actix-web.git" }
|
||||||
|
tera = "*"
|
44
examples/template_tera/src/main.rs
Normal file
44
examples/template_tera/src/main.rs
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
extern crate actix;
|
||||||
|
extern crate actix_web;
|
||||||
|
extern crate env_logger;
|
||||||
|
#[macro_use]
|
||||||
|
extern crate tera;
|
||||||
|
use actix_web::*;
|
||||||
|
|
||||||
|
struct State {
|
||||||
|
template: tera::Tera, // <- store tera template in application state
|
||||||
|
}
|
||||||
|
|
||||||
|
fn index(req: HttpRequest<State>) -> HttpResponse {
|
||||||
|
let s = if let Some(name) = req.query().get("name") { // <- submitted form
|
||||||
|
let mut ctx = tera::Context::new();
|
||||||
|
ctx.add("name", name);
|
||||||
|
ctx.add("text", &"Welcome!".to_owned());
|
||||||
|
req.state().template.render("user.html", &ctx).unwrap()
|
||||||
|
} else {
|
||||||
|
req.state().template.render("index.html", &tera::Context::new()).unwrap()
|
||||||
|
};
|
||||||
|
httpcodes::HTTPOk.build()
|
||||||
|
.content_type("text/html")
|
||||||
|
.body(s)
|
||||||
|
.unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
::std::env::set_var("RUST_LOG", "actix_web=info");
|
||||||
|
let _ = env_logger::init();
|
||||||
|
let sys = actix::System::new("tera-example");
|
||||||
|
|
||||||
|
HttpServer::new(|| {
|
||||||
|
let tera = compile_templates!(concat!(env!("CARGO_MANIFEST_DIR"), "/templates/**/*"));
|
||||||
|
|
||||||
|
Application::with_state(State{template: tera})
|
||||||
|
// enable logger
|
||||||
|
.middleware(middlewares::Logger::default())
|
||||||
|
.resource("/", |r| r.method(Method::GET).f(index))})
|
||||||
|
.bind("127.0.0.1:8080").unwrap()
|
||||||
|
.start().unwrap();
|
||||||
|
|
||||||
|
println!("Started http server: 127.0.0.1:8080");
|
||||||
|
let _ = sys.run();
|
||||||
|
}
|
17
examples/template_tera/templates/index.html
Normal file
17
examples/template_tera/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
examples/template_tera/templates/user.html
Normal file
13
examples/template_tera/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>
|
Loading…
Reference in New Issue
Block a user