Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/lommix/templus

Go inspired html template engine / compiler for rust
https://github.com/lommix/templus

html rust template templates web

Last synced: about 1 month ago
JSON representation

Go inspired html template engine / compiler for rust

Awesome Lists containing this project

README

        

# Templus

### Go inspired template engine/compiler for Rust.

With proper inheritance, composition and caching.

---

Recently, I had the opportunity to develop a small web app in Go utilizing solely the standard libraries.
It is truly remarkable how comprehensive the Go standard library ecosystem is and how far you get with no dependencies at all.

The template behavior was the only truly frustrating aspect for me.
The syntax is acceptable, but the inability to parse all my templates simultaneously, cache the parse tree, and then render specific blocks while maintaining inheritance and composition is disappointing.
Importing items multiple times results in overwrites, and a template can only be inherited once. Go necessitates parsing all required templates for each page in a separate object or reprocessing them with each request.

To challenge myself I implemented my own Go inspired template engine in rust with `serde` being the only dependency.
Syntax is basically the same, but with proper `if` statements, inheritance, and composition.

This is work in progress and will probably be used and iterated on in my next small web project.

```bash
cargo run --example render
```

## Basic Example

Templates are defined by blocks. You can have as many as you want in one file. Html outside define-blocks is ignored.
There is very little cloning, the template container is bound to the lifetime of the template source. Only in the final
render step, the Html gets copied to new Memory. This should make things pretty fast.

```rust
let tmpl = std::fs::read_to_string("templus/examples/example.html").expect("cannot read file");

let mut envirement = templus::renderer::Envirement::new();
envirement.parse(&tmpl).unwrap();

let html = environment
.render(
"foo",
&templus::context! {
name => "lommix",
number => 69,
bool => true
},
)
.unwrap();

print!("{}", html);
```

Sample template code:
```html
{{ define 'base' }}



{{block 'meta'}}
{{ end }}


{{ block 'content' }}
{{ end }}
{{ block 'js' }}

{{ end }}

{{ end }}

{{ define 'foo' extends 'base' }}

{{ block 'meta' }}
jsx sucks
{{end}}

{{ block 'content' }}

hello


{{ import 'foobar' }}

{{ if .bool }}

bool is true


{{end}}

{{ if .number > 42 }}

num is bigger than 42


{{ end }}

{{ if .number < 42 }}

num is smaller than 42


{{ else }}

num is not smaller than 42


{{ end }}

{{ if .number == 69 }}

number is 69


{{ end }}

{{ end }}
{{ end }}

{{ define 'foobar'}}


    {{ range 10 }}
  • you are {{.name}}

  • {{ end }}

{{ end }}
```

## Todos

- Variable assignments.
- Binary operators in if statements.
- User defined functions.
- Bindings for other languages.
- Cli Tools and Parse Tree serialization.