Function hairy::hairy_compile_html

source ·
pub fn hairy_compile_html<'b>(
    reader: &'b str,
    filename: &'b str,
    options: &HairyCompileOptions<'_, '_, '_>
) -> Result<BytecodeVec, String>
Expand description

The ‘easy’ interface if you just want to quickly use a HTML template, with auto escaping of the input, and returning a nicely formatted error that can be presented to the user. For more options, see the hairy_compile function.

To evaluate, use hairy_eval_html, so the proper escaper is used.

Note that although hairy_compile_html and hairy_eval_html are easier to use, they are somewhat slower. For top performance please use other functions.

The inline_overrides argument overrides the inline key-value pairs that can be specified on the top of a template. These keys can contain alphanumeric characters, _, and $. The values should be valid expry expressions (JSON is a valid expry). These values are compiled using the inline_overrides as value, so the inlines and defaults can depend on a (sub)value of compiled (so derived inlines and defaults can be generated).

Example

use hairy::*;
use expry::*;
use expry_macros::*;
use std::io::Write;
let template = r#"foobar = {{=this.foovar .. this.barvar}}"#;
let mut options = HairyOptions::new();
let value = value!({
  "foovar": "foo",
  "barvar": "bar",
}).encode_to_vec();
options.set_named_dynamic_values(&[("this",value.to_ref())]);
let result = hairy_compile_html(template, "test.tpl", &(&options).try_into().unwrap());
match result {
  Ok(parsed) => {
    match hairy_eval_html(parsed.to_ref(), &(&options).try_into().unwrap()) {
      Ok(output) => { std::io::stdout().write_all(&output); },
      Err(err) => { eprintln!("{}", err); },
    }
  },
  Err(err) => {
    eprintln!("{}", err);
    panic!(); // to check this example
  }
}