-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlebars.rs
86 lines (79 loc) · 2.49 KB
/
handlebars.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
use handlebars::{Handlebars, HelperDef};
use serde::Serialize;
use crate::{
error::{Error, ErrorKind, Result},
template::helpers,
};
/// Helper function
///
/// Note:
/// - `&Helper`: current helper template information, contains name, params, hashes and nested template
/// - `&Registry`: the global registry, you can find templates by name from registry
/// - `&Context`: the whole data to render, in most case you can use data from `Helper`
/// - `&mut RenderContext`: you can access data or modify variables (starts with @)/partials in render context, for example, @index of #each. See its document for detail.
/// - `&mut dyn Output`: where you write output to
///
/// # Example
///
/// The following creates an upper case function helper.
///
/// ```rust, ignore
///
/// use handlebars::{
/// Context, Handlebars, Helper, HelperResult, Output, RenderContext,
/// };
///
/// pub fn upper(
/// h: &Helper<'_, '_>,
/// _: &Handlebars<'_>,
/// _: &Context,
/// _rc: &mut RenderContext<'_, '_>,
/// out: &mut dyn Output,
/// ) -> HelperResult {
/// // get parameter from helper or throw an error
/// let param = h.param(0).and_then(|v| v.value().as_str()).unwrap_or("");
/// out.write(param.to_uppercase().as_ref())?;
/// Ok(())
/// }
/// ```
///
/// You can add it to template like so:
///
/// ```rust, ignore
/// use lotlinx::Template;
///
/// let t = Template::new("path/to/src", "path/to/dest");
/// t.re
/// ```
type HelperFn = dyn HelperDef + Send + Sync;
/// Register builtin default Handlebar helpers.
#[inline]
fn register_default_helpers(handlebars: &mut Handlebars) {
// Register handlebars helpers.
register_helper_fn(handlebars, "replace", Box::new(helpers::replace));
register_helper_fn(handlebars, "append", Box::new(helpers::append));
register_helper_fn(handlebars, "prepend", Box::new(helpers::prepend));
register_helper_fn(handlebars, "up", Box::new(helpers::up));
register_helper_fn(handlebars, "low", Box::new(helpers::low));
}
/// Register a new handlebar helper function.
#[inline]
fn register_helper_fn(
hbs: &mut Handlebars,
name: &str,
helper_fn: Box<HelperFn>,
) {
// Register handlebars helpers.
hbs.register_helper(name, helper_fn);
}
pub(crate) fn parse<T: Serialize>(
content: &str,
variables: &T,
) -> Result<String> {
let mut hb = Handlebars::new();
hb.set_strict_mode(true);
// Register default helpers.
register_default_helpers(&mut hb);
hb.render_template(content, variables)
.map_err(|e| Error::new(ErrorKind::ParseError, &e.to_string()))
}