2018-10-13 01:21:14 +01:00
|
|
|
use askama::Template as AskamaTemplate;
|
2018-10-30 02:39:07 +00:00
|
|
|
use store::{ItemList, ItemListTrait};
|
2018-10-13 01:21:14 +01:00
|
|
|
|
|
|
|
#[derive(AskamaTemplate)]
|
|
|
|
#[template(path = "row.html")]
|
|
|
|
struct RowTemplate<'a> {
|
|
|
|
id: &'a str,
|
|
|
|
title: &'a str,
|
|
|
|
completed: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(AskamaTemplate)]
|
|
|
|
#[template(path = "itemsLeft.html")]
|
|
|
|
struct ItemsLeftTemplate {
|
|
|
|
active_todos: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Template {}
|
|
|
|
|
|
|
|
impl Template {
|
|
|
|
/// Format the contents of a todo list.
|
|
|
|
///
|
|
|
|
/// items `ItemList` contains keys you want to find in the template to replace.
|
|
|
|
/// Returns the contents for a todo list
|
|
|
|
///
|
|
|
|
pub fn item_list(items: ItemList) -> String {
|
|
|
|
let mut output = String::from("");
|
|
|
|
for item in items.iter() {
|
|
|
|
let row = RowTemplate {
|
|
|
|
id: &item.id,
|
|
|
|
completed: item.completed,
|
|
|
|
title: &item.title,
|
|
|
|
};
|
|
|
|
if let Ok(res) = row.render() {
|
|
|
|
output.push_str(&res);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
output
|
|
|
|
}
|
|
|
|
|
|
|
|
///
|
|
|
|
/// Format the contents of an "items left" indicator.
|
|
|
|
///
|
|
|
|
/// `active_todos` Number of active todos
|
|
|
|
///
|
|
|
|
/// Returns the contents for an "items left" indicator
|
|
|
|
pub fn item_counter(active_todos: usize) -> String {
|
2018-10-30 02:39:07 +00:00
|
|
|
let items_left = ItemsLeftTemplate { active_todos };
|
2018-10-13 01:21:14 +01:00
|
|
|
if let Ok(res) = items_left.render() {
|
|
|
|
res
|
|
|
|
} else {
|
|
|
|
String::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|