mirror of https://github.com/rwf2/Rocket.git
Update cookies example to use contrib Template.
This commit is contained in:
parent
a7b180c911
commit
7c6dce266b
|
@ -8,4 +8,8 @@ workspace = "../../"
|
|||
rocket = { path = "../../lib" }
|
||||
rocket_codegen = { path = "../../codegen" }
|
||||
lazy_static = "*"
|
||||
tera = { git = "https://github.com/Keats/tera" }
|
||||
|
||||
[dependencies.rocket_contrib]
|
||||
path = "../../contrib"
|
||||
default-features = false
|
||||
features = ["tera_templates"]
|
||||
|
|
|
@ -3,20 +3,14 @@
|
|||
|
||||
#[macro_use]
|
||||
extern crate lazy_static;
|
||||
extern crate rocket_contrib;
|
||||
extern crate rocket;
|
||||
extern crate tera;
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
use rocket::response::Redirect;
|
||||
use rocket::http::{Cookie, Cookies};
|
||||
|
||||
lazy_static!(static ref TERA: tera::Tera = tera::Tera::new("templates/**/*"););
|
||||
|
||||
fn ctxt(message: Option<String>) -> tera::Context {
|
||||
let mut context = tera::Context::new();
|
||||
context.add("have_message", &message.is_some());
|
||||
context.add("message", &message.unwrap_or("".to_string()));
|
||||
context
|
||||
}
|
||||
use rocket_contrib::Template;
|
||||
|
||||
#[derive(FromForm)]
|
||||
struct Message {
|
||||
|
@ -30,9 +24,13 @@ fn submit(cookies: &Cookies, message: Message) -> Redirect {
|
|||
}
|
||||
|
||||
#[get("/")]
|
||||
fn index(cookies: &Cookies) -> tera::TeraResult<String> {
|
||||
let message = cookies.find("message").map(|msg| msg.value);
|
||||
TERA.render("index.html", ctxt(message))
|
||||
fn index(cookies: &Cookies) -> Template {
|
||||
let mut context = HashMap::new();
|
||||
if let Some(msg) = cookies.find("message").map(|msg| msg.value) {
|
||||
context.insert("message", msg);
|
||||
}
|
||||
|
||||
Template::render("index", &context)
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
</head>
|
||||
<body>
|
||||
<h1>Rocket Cookie Examples</h1>
|
||||
{% if have_message %}
|
||||
{% if message is defined %}
|
||||
<p>{{ message }}</p>
|
||||
{% else %}
|
||||
<p>No message yet.</p>
|
Loading…
Reference in New Issue