diff options
author | Harald Eilertsen <haraldei@anduin.net> | 2017-11-16 16:40:38 +0100 |
---|---|---|
committer | Harald Eilertsen <haraldei@anduin.net> | 2017-11-16 16:40:38 +0100 |
commit | 3bc7143ced57da953969d80d99ff8466ab986ae2 (patch) | |
tree | a4a8ef0958a1c9dae5b6f09f4642d83c3d756fe3 | |
parent | 1767eb2dafbab8b19cc4cf0a9a890539ce2948c1 (diff) | |
download | rocket-blog-3bc7143ced57da953969d80d99ff8466ab986ae2.tar.gz rocket-blog-3bc7143ced57da953969d80d99ff8466ab986ae2.tar.bz2 rocket-blog-3bc7143ced57da953969d80d99ff8466ab986ae2.zip |
Add Bart templating engine.
-rw-r--r-- | Cargo.toml | 2 | ||||
-rw-r--r-- | src/main.rs | 25 | ||||
-rw-r--r-- | templates/index.html | 9 |
3 files changed, 26 insertions, 10 deletions
@@ -11,3 +11,5 @@ diesel_codegen = { version = "0.16.0", features = ["postgres"] } dotenv = "0.9.0" r2d2 = "0.7.4" r2d2-diesel = "0.16.0" +bart = "0.1.4" +bart_derive = "0.1.4" diff --git a/src/main.rs b/src/main.rs index 622ac1c..8dd6771 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,27 +1,32 @@ #![feature(plugin)] #![plugin(rocket_codegen)] +extern crate diesel; extern crate rocket; extern crate rocket_blog; +#[macro_use] extern crate bart_derive; -extern crate diesel; use self::diesel::prelude::*; +use self::rocket_blog::{schema, models}; + +#[derive(BartDisplay)] +#[template = "templates/index.html"] +struct IndexTemplate<'a> { + title: &'a str, + content: &'a str +} #[get("/")] fn index(conn: rocket_blog::DbConn) -> String { - use rocket_blog::schema::posts::dsl::*; + use schema::posts::dsl::*; let results = posts.filter(published.eq(false)) .limit(5) - .load::<rocket_blog::models::Post>(&*conn) + .load::<models::Post>(&*conn) .expect("Error loading posts"); - let mut output = String::from("Hallo, for faen!\n\n"); - - for post in results { - output += &format!("{}\n", post.title); - output += "================\n"; - output += &format!("{}\n", post.body); - } + let output : String = results.iter().map(|post| { + format!("{}", &IndexTemplate{ title: &post.title, content: &post.body }) + }).collect(); output } diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..c2a7b92 --- /dev/null +++ b/templates/index.html @@ -0,0 +1,9 @@ +<!DOCTYPE html> +<html> + <head> + <title>{{ title }}</title> + </head> + <body> + {{ content }} + </body> +</html>
\ No newline at end of file |