let mut grid = zakgrid::Grid::new();
- let mut s = format!("before: {}\n", grid.header_tmpl);
-
- grid.header_tmpl = String::from("new header");
-
- s.push_str(format!("after: {}\n", grid.header_tmpl).as_str());
+ let mut s = String::new();
let mut col = zakgrid::Column::new("col name");
s.push_str(format!("column added: {}\n", col.name()).as_str());
s.push_str(format!("col {} title {}\n", i, v.title()).as_str());
}
+ let header = grid.header();
+ s.push_str(header.as_str());
+
Ok(HttpResponse::Ok().body(s))
}
+use serde_derive::Serialize;
+use tera::Tera;
+
+#[derive(Debug, Serialize)]
pub enum Align {
Left,
Center,
}
pub struct Grid {
- pub header_tmpl: String,
+ header_tmpl: String,
header_row_tmpl: String,
row_tmpl: String,
row_col_tmpl: String,
impl Grid {
pub fn new() -> Grid {
Grid {
- header_tmpl: String::new(),
- header_row_tmpl: String::new(),
+ header_tmpl: String::from("<tr>\n{{ row }}</tr>\n"),
+ header_row_tmpl: String::from("\t<th{% if align == \"Center\" %} align='center'{% elif align == \"Right\" %} align='right'{% endif %}>{{ title }}</th>\n"),
row_tmpl: String::new(),
row_col_tmpl: String::new(),
columns: vec![]
pub fn columns(&self) -> &Vec<Column> {
&self.columns
}
+
+ pub fn header(&self) -> String {
+ let mut s = String::new();
+
+ let mut tera = Tera::default();
+
+ tera.add_raw_template("header", &self.header_tmpl);
+ tera.add_raw_template("header_row", &self.header_row_tmpl);
+
+ for c in &self.columns {
+ if !c.hidden {
+ let mut context = tera::Context::new();
+ context.insert("title", &c.title);
+ context.insert("align", &c.align);
+ s.push_str(tera.render("header_row", &context).unwrap().as_str());
+ }
+ }
+
+ let mut context = tera::Context::new();
+ context.insert("row", s.as_str());
+ String::from(tera.render("header", &context).unwrap().as_str())
+ }
}