48 releases

0.17.2 Jun 30, 2024
0.17.0 Jul 22, 2023
0.16.1 Jan 28, 2023
0.15.0 Sep 18, 2022
0.1.2 Nov 20, 2016

#24 in Template engine

Download history 151/week @ 2024-07-19 523/week @ 2024-07-26 287/week @ 2024-08-02 292/week @ 2024-08-09 198/week @ 2024-08-16 201/week @ 2024-08-23 244/week @ 2024-08-30 218/week @ 2024-09-06 192/week @ 2024-09-13 1877/week @ 2024-09-20 259/week @ 2024-09-27 150/week @ 2024-10-04 282/week @ 2024-10-11 237/week @ 2024-10-18 77/week @ 2024-10-25 153/week @ 2024-11-01

777 downloads per month
Used in 7 crates

MIT/Apache

105KB
2K SLoC

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.

Crate docs CI

Design criteria

  • 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 Display trait should be outputable.
  • By default, all values should be html-escaped. There should be an easy but explicit way to output preformatted html.

Current status

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.

Template format

A template consists of three basic parts: First a preamble of use statements, each prepended by an @ sign. 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:

@use any::rust::Type;
@use super::statics::style_css;

@(name: &str, items: &[Type])

<html>
   <head>
     <title>@name</title>
     <link rel="stylesheet" href="/static/@style_css.name" type="text/css"/>
   </head>
   <body>
     <h1>@name</h1>
     <dl>
     @for item in items {
       <dt>@item.title()</dt>
       <dd>@item.description()</dd>
     }
     </dl>
   <body>
</html>

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 axum, actix-web, gotham, iron, nickel, tide, and warp. There is also a separate example of using ructe with warp and diesel.

Dependencies

~1.5–2.1MB
~41K SLoC