From 7c6dce266b248235b45c68f1dc7999e6e29ee6be Mon Sep 17 00:00:00 2001 From: Sergio Benitez Date: Sat, 8 Oct 2016 21:02:42 -0700 Subject: [PATCH] Update cookies example to use contrib Template. --- examples/cookies/Cargo.toml | 6 ++++- examples/cookies/src/main.rs | 24 +++++++++---------- .../templates/{index.html => index.html.tera} | 2 +- 3 files changed, 17 insertions(+), 15 deletions(-) rename examples/cookies/templates/{index.html => index.html.tera} (94%) diff --git a/examples/cookies/Cargo.toml b/examples/cookies/Cargo.toml index b805624d..8ff982a0 100644 --- a/examples/cookies/Cargo.toml +++ b/examples/cookies/Cargo.toml @@ -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"] diff --git a/examples/cookies/src/main.rs b/examples/cookies/src/main.rs index 13d24e67..7bb4470e 100644 --- a/examples/cookies/src/main.rs +++ b/examples/cookies/src/main.rs @@ -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) -> 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 { - 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() { diff --git a/examples/cookies/templates/index.html b/examples/cookies/templates/index.html.tera similarity index 94% rename from examples/cookies/templates/index.html rename to examples/cookies/templates/index.html.tera index f222a990..1ec908d4 100644 --- a/examples/cookies/templates/index.html +++ b/examples/cookies/templates/index.html.tera @@ -7,7 +7,7 @@

Rocket Cookie Examples

- {% if have_message %} + {% if message is defined %}

{{ message }}

{% else %}

No message yet.