summaryrefslogtreecommitdiffstats
path: root/vendor/handlebars/tests/data_helper.rs
blob: 7734b0d7a7520a95d93c3c7117f4f82d19a33d7d (plain)
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
use handlebars::*;
use serde_json::json;

struct HelperWithBorrowedData<'a>(&'a String);

impl<'a> HelperDef for HelperWithBorrowedData<'a> {
    fn call<'_reg: '_rc, '_rc>(
        &self,
        _: &Helper<'_reg, '_rc>,
        _: &'_reg Handlebars,
        _: &Context,
        _: &mut RenderContext,
        out: &mut dyn Output,
    ) -> Result<(), RenderError> {
        out.write(self.0).map_err(RenderError::from)
    }
}

#[test]
fn test_helper_with_ref_data() {
    let s = "hello helper".to_owned();
    let the_helper = HelperWithBorrowedData(&s);

    let mut r = Handlebars::new();
    r.register_helper("hello", Box::new(the_helper));

    let s = r.render_template("Output: {{hello}}", &json!({})).unwrap();
    assert_eq!(s, "Output: hello helper".to_owned());
}