templates.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
use crate::errors::{ Result };
use crate::state::{ ApplicationState };
use serde::{ Serialize };
#[derive(Serialize)]
struct ApplicationContext {
name: String,
version: String,
}
#[derive(Serialize)]
struct PageContext {
title: String,
content: String,
}
pub(crate) fn index_context(state: &ApplicationState) -> Result<tera::Context> {
let mut context = tera::Context::new();
add_application_context(&mut context, &state);
add_page_context(&mut context, &state, "index")?;
Ok(context)
}
fn add_application_context(context: &mut tera::Context, state: &ApplicationState) {
let application_context = ApplicationContext {
name: state.name.clone(),
version: env!("CARGO_PKG_VERSION").to_string(),
};
context.insert("application", &application_context);
}
fn add_page_context(context: &mut tera::Context, state: &ApplicationState, page_name: &str) -> Result<()> {
let page = state.content_provider.find_page(page_name)?;
let page_context = PageContext {
title: page.title,
content: page.content,
};
context.insert("page", &page_context);
Ok(())
}