| 1 | use handlebars::*; |
| 2 | use serde_json::json; |
| 3 | |
| 4 | struct HelperWithBorrowedData<'a>(&'a String); |
| 5 | |
| 6 | impl<'a> HelperDef for HelperWithBorrowedData<'a> { |
| 7 | fn call<'_reg: '_rc, '_rc>( |
| 8 | &self, |
| 9 | _: &Helper<'_rc>, |
| 10 | _: &'_reg Handlebars, |
| 11 | _: &Context, |
| 12 | _: &mut RenderContext, |
| 13 | out: &mut dyn Output, |
| 14 | ) -> Result<(), RenderError> { |
| 15 | out.write(self.0).map_err(RenderError::from) |
| 16 | } |
| 17 | } |
| 18 | |
| 19 | #[test] |
| 20 | fn test_helper_with_ref_data() { |
| 21 | let s = "hello helper".to_owned(); |
| 22 | let the_helper = HelperWithBorrowedData(&s); |
| 23 | |
| 24 | let mut r = Handlebars::new(); |
| 25 | r.register_helper("hello", Box::new(the_helper)); |
| 26 | |
| 27 | let s = r.render_template("Output: {{hello}}", &json!({})).unwrap(); |
| 28 | assert_eq!(s, "Output: hello helper".to_owned()); |
| 29 | } |