Skip to content

Instantly share code, notes, and snippets.

@bpremo
Last active June 12, 2020 18:47
Show Gist options
  • Select an option

  • Save bpremo/f552835fec1efab82cda53d95dddb8c2 to your computer and use it in GitHub Desktop.

Select an option

Save bpremo/f552835fec1efab82cda53d95dddb8c2 to your computer and use it in GitHub Desktop.
extern crate tera;
extern crate serde;
use tera::Tera;
use tera::Context;
use serde::{Serialize};
#[derive(Serialize)]
struct Person {
name: String,
action: String,
}
fn main() {
let scott = Person{name:String::from("Scott"), action:String::from("sucks")};
let brandon = Person{name:String::from("Brandon"), action:String::from("ROCKS")};
let people = vec![&scott, &brandon];
let mut tera = Tera::default();
let _ = match tera.add_raw_template("test", "{{ person.name }} {{ person.action }}!") {
Ok(t) => t,
Err(e) => {
println!("Parsing error(s): {}", e);
::std::process::exit(1);
}
};
for person in people.iter() {
let mut context = Context::new();
context.insert("person", &person);
let res = match tera.render("test", &context) {
Ok(t) => t,
Err(e) => {
println!("Parsing error(s): {}", e);
::std::process::exit(1);
}
};
println!("{}", res)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment