From b79cbc771457e52feb584c59fd70370237665be8 Mon Sep 17 00:00:00 2001 From: Rasmus Kaj Date: Fri, 23 Sep 2016 16:40:54 +0200 Subject: [PATCH] More ructe templates. --- src/main.rs | 91 ++++++++++++++++++++++------------------ templates/groups.rs.html | 7 ++-- templates/groups.tpl | 24 ----------- templates/head.rs.html | 9 ++-- templates/index.rs.html | 29 +++++++++++++ templates/index.tpl | 18 -------- 6 files changed, 86 insertions(+), 92 deletions(-) delete mode 100644 templates/groups.tpl create mode 100644 templates/index.rs.html delete mode 100644 templates/index.tpl diff --git a/src/main.rs b/src/main.rs index 0de8c52..45f5d67 100644 --- a/src/main.rs +++ b/src/main.rs @@ -38,7 +38,6 @@ use diesel::expression::sql_literal::SqlLiteral; use diesel::prelude::*; use diesel::pg::PgConnection; use chrono::naive::date::NaiveDate; -use std::str::from_utf8; use rphotos::models::{Camera, Person, Photo, Place, Tag}; @@ -513,10 +512,7 @@ fn all_years<'mw>(req: &mut Request, }).collect(); let mut stream = try!(res.start()); - // TODO Use a proper sub-template - let mut headvec = Vec::new(); - templates::head(&mut headvec, Vec::new()).unwrap(); - match templates::groups(&mut stream, "All photos", Html(from_utf8(&headvec).unwrap()), groups) { + match templates::groups(&mut stream, "All photos", Vec::new(), user, groups) { Ok(()) => Ok(Halt(stream)), Err(e) => stream.bail(format!("Problem rendering template: {:?}", e)) } @@ -566,10 +562,7 @@ fn months_in_year<'mw>(req: &mut Request, }).collect(); let mut stream = try!(res.start()); - // TODO Use a proper sub-template - let mut headvec = Vec::new(); - templates::head(&mut headvec, Vec::new()).unwrap(); - match templates::groups(&mut stream, &title, Html(from_utf8(&headvec).unwrap()), groups) { + match templates::groups(&mut stream, &title, Vec::new(), user, groups) { Ok(()) => Ok(Halt(stream)), Err(e) => stream.bail(format!("Problem rendering template: {:?}", e)) } @@ -583,11 +576,10 @@ fn days_in_month<'mw>(req: &mut Request, use rphotos::schema::photos::dsl::{date, grade}; let c: &PgConnection = &req.db_conn(); - render!(res, "templates/groups.tpl", { - user: Option = req.authorized_user(), - lpath: Vec = vec![Link::year(year)], - title: String = format!("Photos from {} {}", monthname(month), year), - groups: Vec = + let user: Option = req.authorized_user(); + let lpath: Vec = vec![Link::year(year)]; + let title: String = format!("Photos from {} {}", monthname(month), year); + let groups: Vec = SqlLiteral::new(format!( "select cast(extract(day from date) as int) d, count(*) c \ from photos where extract(year from date)={} \ @@ -616,8 +608,13 @@ fn days_in_month<'mw>(req: &mut Request, count: count, photo: photo } - }).collect() - }) + }).collect(); + + let mut stream = try!(res.start()); + match templates::groups(&mut stream, &title, lpath, user, groups) { + Ok(()) => Ok(Halt(stream)), + Err(e) => stream.bail(format!("Problem rendering template: {:?}", e)) + } } fn all_null_date<'mw>(req: &mut Request, @@ -626,16 +623,19 @@ fn all_null_date<'mw>(req: &mut Request, use rphotos::schema::photos::dsl::{date, path}; let c: &PgConnection = &req.db_conn(); - render!(res, "templates/index.tpl", { - user: Option = req.authorized_user(), - lpath: Vec = vec![], - title: &'static str = "Photos without a date", - photos: Vec = Photo::query(req.authorized_user().is_some()) - .filter(date.is_null()) - .order(path.asc()) - .limit(500) - .load(c).unwrap() - }) + let mut stream = try!(res.start()); + match templates::index(&mut stream, + &"Photos without a date", + vec![], + req.authorized_user(), + Photo::query(req.authorized_user().is_some()) + .filter(date.is_null()) + .order(path.asc()) + .limit(500) + .load(c).unwrap()) { + Ok(()) => Ok(Halt(stream)), + Err(e) => stream.bail(format!("Problem rendering template: {:?}", e)) + } } fn all_for_day<'mw>(req: &mut Request, @@ -648,19 +648,24 @@ fn all_for_day<'mw>(req: &mut Request, use rphotos::schema::photos::dsl::{date, grade}; let c: &PgConnection = &req.db_conn(); - render!(res, "templates/index.tpl", { - user: Option = req.authorized_user(), - lpath: Vec = vec![Link::year(year), - Link::month(year, month)], - title: String = format!("Photos from {} {} {}", - day, monthname(month), year), - photos: Vec = Photo::query(req.authorized_user().is_some()) + + let user: Option = req.authorized_user(); + let lpath: Vec = vec![Link::year(year), + Link::month(year, month)]; + let title: String = format!("Photos from {} {} {}", + day, monthname(month), year); + let photos: Vec = Photo::query(req.authorized_user().is_some()) .filter(date.ge(thedate)) .filter(date.lt(thedate + ChDuration::days(1))) .order((grade.desc(), date.asc())) .limit(500) - .load(c).unwrap() - }) + .load(c).unwrap(); + + let mut stream = try!(res.start()); + match templates::index(&mut stream, &title, lpath, user, photos) { + Ok(()) => Ok(Halt(stream)), + Err(e) => stream.bail(format!("Problem rendering template: {:?}", e)) + } } fn on_this_day<'mw>(req: &mut Request, @@ -673,11 +678,12 @@ fn on_this_day<'mw>(req: &mut Request, let now = time::now(); (now.tm_mon as u32 + 1, now.tm_mday as u32) }; - render!(res, "templates/groups.tpl", { - user: Option = req.authorized_user(), - title: String = format!("Photos from {} {}", day, monthname(month)), - groups: Vec = - SqlLiteral::new(format!( + let mut stream = try!(res.start()); + match templates::groups(&mut stream, + &format!("Photos from {} {}", day, monthname(month)), + vec![], + req.authorized_user(), + SqlLiteral::new(format!( "select extract(year from date) y, count(*) c \ from photos where extract(month from date)={} \ and extract(day from date)={}{} group by y order by y desc", @@ -706,7 +712,10 @@ fn on_this_day<'mw>(req: &mut Request, photo: photo } }).collect() - }) + ) { + Ok(()) => Ok(Halt(stream)), + Err(e) => stream.bail(format!("Problem rendering template: {:?}", e)) + } } #[derive(Debug, Clone, RustcEncodable)] diff --git a/templates/groups.rs.html b/templates/groups.rs.html index d1b19de..38f1416 100644 --- a/templates/groups.rs.html +++ b/templates/groups.rs.html @@ -1,6 +1,7 @@ -@use ::{CSSLINK, Group}; +@use ::{CSSLINK, Group, Link}; +@use templates::head; -@(title: &str, head: Html<&str>, groups: Vec) +@(title: &str, lpath: Vec, user: Option, groups: Vec) @@ -10,7 +11,7 @@ @CSSLINK - @head + @:head(lpath, user)

@title

diff --git a/templates/groups.tpl b/templates/groups.tpl deleted file mode 100644 index 09db64a..0000000 --- a/templates/groups.tpl +++ /dev/null @@ -1,24 +0,0 @@ - - - - {{title}} - - {{{csslink}}} - - - {{> head}} -

{{title}}

- -
- {{#groups}} -

{{title}}

-

-

{{count}} pictures

-
- {{/groups}} - {{^groups}} -

Inga bilder.

- {{/groups}} -
- - diff --git a/templates/head.rs.html b/templates/head.rs.html index 4f5d0f9..91657ba 100644 --- a/templates/head.rs.html +++ b/templates/head.rs.html @@ -1,6 +1,6 @@ @use ::Link; -@(lpath: Vec) +@(lpath: Vec, user: Option)
Bilder @@ -10,9 +10,6 @@ · Personer · Platser · Denna dag -@* {{#user}}{{.}} -(log out) -{{/user}} -{{^user}}(log in){{/user}} -*@ +@if let Some(u) = user {@u (log out)} +else {(log in)}
diff --git a/templates/index.rs.html b/templates/index.rs.html new file mode 100644 index 0000000..a148d13 --- /dev/null +++ b/templates/index.rs.html @@ -0,0 +1,29 @@ +@use ::{CSSLINK, Link}; +@use rphotos::models::Photo; +@use templates::head; + +@(title: &str, lpath: Vec, user: Option, photos: Vec) + + + + + @title + + @CSSLINK + + + @:head(lpath, user) +

@title

+ +
+ @for p in photos { +

+ } + @* + {{^photos}} +

Inga bilder.

+ {{/photos}} + *@ +
+ + diff --git a/templates/index.tpl b/templates/index.tpl deleted file mode 100644 index f02fa7d..0000000 --- a/templates/index.tpl +++ /dev/null @@ -1,18 +0,0 @@ - - - - {{title}} - - {{{csslink}}} - - - {{> head}} -

{{title}}

- -
- {{#photos}} -

- {{/photos}} -
- -