hatter 0.0.1

Positively mad, zero dependency HTML templates.
Documentation

~( Hatter: A x̷̨̼͕͖̼͆͑̈̈́́̈́̓̓͊̐́͋̈̈́̐͗́̂̆̄̑̚͝x̶̢̬͚̥̬̦͇̑̀͗͜͜x̸̛̮͎͉̩̰̥̳͓̥͕̱͓͕̩̻̬̙̹̀̿̋̏́́̐̋̈́̔̓͆͛͊̾͋̍̚͝͠͠x̵̧̥̜̅̋̇͋̈́͑̄̾͒̔̑ HyperText Library )

Hatter is a slightly mad, zero dependency HTML template library.

If you are looking for any of the following:

  • Blazing fast speed
  • Templates that compile to Rust
  • Generating HTML from Rust code
  • Type safety

Then you are in the wrong place!

Hatter is an HTML template library that can only do a few things:

  • Print values (bool, string, i32)
  • Run helper functions
  • Loop over a Vec or HashMap
  • if/else

That's it. You should definitely be using a different template library.

~( printing values )

let mut env = Env::new();
env.set("name", "Bobert, but call me Bob");
let html = env.render("values.html")?;
<a href="/profile">~( name )</a>

Or the shorthand version, for variables only:

<a href="/profile">~name</a>

~( helper functions )

let mut env = Env::new();
env.set("a", 100);
env.set("b", 200);
env.helper("add", |_, args| (args[0].as_num() + args[1].as_num()).into());
env.render("helper.html");
<p>According to my calculations, ~a + ~b = <strong>~( add(a, b) )</strong>!</p>

~( looping )

let mut env = Env::new();
let mut pages = vec!["help", "about", "version", "bananas"];
env.set("pages", pages);
env.helper("to_title", |_, args| args[0].as_str().capitalize().into());
let html = env.render("loop.html")?;
<ul>
  ~( for page in pages )
    <li><a href="~page">~( to_title(page) )</a></li>
  ~( end )
</ul>

~( conditionals )

let mut env = Env::new();
let mut pages = vec!["help", "about", "version", "bananas"];
let mut map = HashMap::new();
map.insert("name", "Bobby");
env.set("user", map);
env.helper("logged_in", |e,_| current_user == e.get("user").unwrap().as_str())
let html = env.render("cond.html")?;
~( if logged_in? )
  <a href="/profile">~( user.name )</a>
~( else )
  <a href="/login">Log in</a>
~( end )

All together now

let mut env = Env::new();
env.set("pages", vec!["index", "show", "edit"]);
env.set("logged_in", true);
env.helper("to_title", |_, args| capitalize(args[0].as_str()));

let mut user = HashMap::new();
user.insert("name", "htmlkid99");
user.insert("location", "idaho");
env.set("user", user);

let out = env.render("index.html")?;
println!("{}", out);