| Crates.io | hatter |
| lib.rs | hatter |
| version | 0.1.4 |
| created_at | 2020-05-28 07:34:32.177009+00 |
| updated_at | 2020-10-23 02:35:41.665087+00 |
| description | positively mad, zero dependency html templating language |
| homepage | https://hatter.rs |
| repository | https://github.com/xvxx/hatter |
| max_upload_size | |
| id | 246955 |
| size | 159,485 |
It is practically impossible to teach good programming to students that have had a prior exposure to Hatter: as potential programmers they are mentally mutilated beyond hope of regeneration.
-– Edsger W. Dijkstra (allegedly)
Hatter is a small, whitespace sensitive templating language with HTML support built right in. Its HTML features and syntax are a cheap knock off of Imba, except Hatter produces raw, static HTML - no JavaScript in sight.
Hatter can be used to generate static web sites or to render server side content in a good ol' fashioned web application - either with Vial or your Rust web framework of choice.
If you're feeling adventerous, or mad as a hatter, you can use the standalone binary to turn templates into HTML files, or include the zero-dependency Rust library in your (web/cli/?) application.
Here are a few basic examples of what Hatter looks like and the HTML it generates:
<!-- Hatter -->
<#main> Hi there!
<!-- Generated HTML -->
<div id='main'>Hi there!</div>
<span.big.bold> Welcome!
<span class='big bold'>Welcome!</span>
<.links> for link in nav-links
<a href={link.href}> link.text
<div class='links'>
<a href='/link1'>First Link</a>
<a href='/link2'>2nd Link</a>
<a href='/link3'>Final Link</a>
</div>
<form GET="/search">
<input@query:text placeholder="Search..." /> <input:submit />
<form method='GET' action='/search'>
<input name='query' type='text' placeholder='Search...' />
<input type='submit' />
</form>
<h1> Welcome, <i> Rob becomes <h1> Welcome, <i> Rob </i></h1>id, class, type, and name attributes:
<div#id><div.class1.class2><input@form-field-name><input:text><div page-num={page.id}><div .logged-in=logged-in?><div data-map=is-map?><span.greeting> "Hey there {name}. 2 + 2 is {2 + 2}"<span #page-{page.id} .is-{page.type}> page.title<#main> becomes <div id='main'><i>delicious</> becomes <i>delicious</i><li> <a onclick=(alert("Oink!"))> "🐷"bool, int, float, string, list, map, fnlist and map:
<ul> for page in pages do <li id=page-{page.id}> page.namefor k, v in some-map do <td> k </> <td> vif logged_in? then <h2> Welcome back!:= and =:
name := 'Bob' will error if name is already set.name = 'Bob' will error if name isn't already set.<div.name> to-uppercase(name)def greet(name) do print("Hey there, {name}!")greet("Lydia") prints Hey there, Lydia!def ++(a, b) do concat(to-uppercase(a), ' ', to-uppercase(b))"one" ++ "two" returns ONE TWOadder := fn(x) fn(y) x + y then add1 := adder(1)add1(200) returns 201def greet(title, name) do print("Hiya, {title}. {name}!")greet(name: "Marley", title: "Dr") prints Hiya, Dr. Marley!do keyword for one-line blocks:
if 2 > 1 do print("Obviously")for x in list do print(x)then keyword for one-line if statements:
if 2 > 1 then print("Yup!") else if 2 < 1 then print("Impossible.")<!DOCTYPE> and wrap everything in <html> if
the first tag in your template is <head>.There are two ways to use Hatter:
Hatter can (primarily) be used as a templating language from within your Rust applications.
Simply add Hatter to Cargo.toml:
[dependencies]
hatter = "0.1"
Then create a hatter::Env, which represents the top-level Hatter
scope for your template, to set variables and render your template:
use hatter::{Args, Env, Value};
let mut env = Env::new();
env.set("name", "Bobby Boucher");
env.set("age", 31);
env.render(r#"
<p> <b>Name:</> name
<p> <b>Age:</> age
"#)
You can also write functions in Rust and make them available to your HTML templates:
use hatter::prelude::*;
fn quote(args: Args) -> Result<Value> {
let file = std::fs::read_to_string("quotes.txt")?;
let list_of_quotes: Vec<_> = file.split('\n').collect();
let line = match args.need_number(0)? as usize {
n if n > list_of_quotes.len() => 0,
n => n,
};
Value::from(list_of_quotes[line]).ok()
}
fn main() {
let mut env = Env::new();
env.set("quote", quote);
println!("{}", env.render("<div> quote(1)").unwrap());
}
For more infomation see the API Documentation.
Hatter can be used as a regular command line program to turn .hat
files into HTML.
Just install it using cargo:
cargo install hatter
Then point it at any .hat file:
$ cat test.hat
<b.test> "Testing 1 2 3 {2 + 2}"
$ hatter test.hat
<b class='test'>Testing 1 2 3 4 </b>
You can also install Hatter with a readline-powered REPL:
cargo install hatter --features repl
To launch it, start hatter with no arguments:
$ hatter
Hatter v0.0.1 REPL
>> 1 + 2
3
def <item(item)> do <li.item data-id={item.id}> item.text.Hatter is licensed under either of the following, at your pleasure:
Imba is licensed under the MIT License.