Rust Compiled Templates — ructe
This is my attempt at writing a HTML template system for Rust. Some inspiration comes from the scala template system used in play 2, as well as plain old jsp.
- As many errors as possible should be caught at compile-time.
- A compiled binary should include all the template code it needs, no need to read template files at runtime.
- Compilation may take time, running should be fast.
- Writing templates should be almost as easy as writing html.
- The template language should be as expressive as possible.
- It should be possible to write templates for any text-like format, not only html.
- Any value that implements the
Displaytrait should be outputable.
- By default, all values should be html-escaped. There should be an easy but explicit way to output preformatted html.
Ructes is in a rather early stage, but does work; templates can be transpiled to rust functions, which are then compiled and can be called from rust code.
A template consists of three basic parts:
First a preamble of
use statements, each prepended by an
Secondly a declaration of the parameters the template takes.
And third, the template body.
The full syntax is described in the documentation. Some examples can be seen in examples/simple/templates. A template may look something like this:
How to use ructe
Ructe compiles your templates to rust code that should be compiled with your other rust code, so it needs to be called before compiling, as described in the documentation. There are also examples, both for ructe itself and its futures and for using it with the web frameworks actix-web, gotham, iron, nickel, tide, and warp. There is also a separate example of using ructe with warp and diesel.