Crate ructe

source ·
Expand description

Rust Compiled Templates is a HTML template system for Rust.

Ructe works by converting your templates (and static files) to rust source code, which is then compiled with your project. This has the benefits that:

  1. Many syntactical and logical errors in templates are caught compile-time, rather than in a running server.
  2. No extra latency on the first request, since the template are compiled before starting the program.
  3. The template files does not have to be distributed / installed. Templates (and static assets) are included in the compiled program, which can be a single binary.

The template syntax, which is inspired by Twirl, the Scala-based template engine in Play framework, is documented in the Template syntax module. A sample template may look like this:

@use ::Group;
@use super::page_base;

@(title: &str, user: Option<String>, groups: &[Group])

@:page_base(title, &user, {
  <div class="group">
    @if groups.is_empty() {
      <p>No pictures.</p>
    }
    @for g in groups {
      <div class="item"><h2>@g.title</h2>
        <p><a href="@g.url"><img src="/img/@g.photo.id-s.jpg"></a></p>
        <p>@g.count pictures</p>
      </div>
    }
  </div>
})

There are some examples in the repository. There is also a separate example of using ructe with warp and diesel.

Modules

This module describes how to configure and use Rust Compiled Templates.
This module describes the template syntax used by ructe.
Apart from handling templates for dynamic content, ructe also helps with constants for static content.
The module containing your generated template code will also contain everything from here.

Structs

Handler for static files.

Functions

Create a statics module inside outdir, containing static file data for all files in indir.
Create a templates module in outdir containing rust code for all templates found in indir.