Liquid Hello World read template from file
-
parse_file
-
Templates are usually much biggger than what we had in the first example.
-
We usually prefer to keep the templates as an external files.
-
Instead of
parsewe can useparse_fileto load the template from an external file. -
This happens at run-time so you will need to supply the templates outside of the binary or the user will need to create the templates.
Hello Liquid!
fn main() { let filename = "template.txt"; let template = liquid::ParserBuilder::with_stdlib() .build() .unwrap() .parse_file(filename) .unwrap(); let name = String::from("Liquid"); let globals = liquid::object!({ "name": name }); let output = template.render(&globals).unwrap(); println!("{}", output); }
{% embed include file="src/examples/liquid/liquid-hello-world-from-file/template.txt)