Note: This site is currently "Under construction". I'm migrating to a new version of my site building software. Lots of things are in a state of disrepair as a result (for example, footnote links aren't working). It's all part of the process of building in public. Most things should still be readable though.

Use Rust Struct Functions In A Sailfish Template

I'm working on the template engine for my static site generator. I've been using Minijinga. The base site builds in 2 seconds. Pretty good for 2K files. I'm curious to see if I can make it go faster. My next step is to experiment with Sailfish.

A critical factor for me is being able to call functions on the structs that are being used to render. That took a while to figure out in Minijinja. This is how I'm doing it in Sailfish:

Cargo.toml

[dependencies]
sailfish = "0.8.0"

templates/hello.stpl

<html>
  <body>
  <h2>Direct Key Access To Vec</h2>
  <% for msg in &messages { %>
    <div><%= msg %></div>
  <% } %>
  <h2>Function With Single String</h2>
    <div><%= h.ping() %></div>
  <h2>Function With Vec</h2>
  <% for item in h.runner() {%>
    <div><%= item %></div>
  <% } %>
  </body>
</html>

src/main.rs

use sailfish::TemplateOnce;

#[derive(TemplateOnce)]
#[template(path = "hello.stpl")]
struct HelloTemplate {
    messages: Vec<String>,
    h: Holder,
}

struct Holder {}

impl Holder {
    pub fn ping(&self) -> String {
        "one ping only".to_string()
    }
    pub fn runner(&self) -> Vec<String> {
        vec![
            "alfa".to_string(),
            "bravo".to_string(),
            "charlie".to_string(),
            "delta".to_string(),
        ]
    }
}

fn main() {
    let ctx = HelloTemplate {
        messages: vec![String::from("foo"), String::from("bar")],
        h: Holder {},
    };
    println!("{}", ctx.render_once().unwrap());
}

References

  • My static site generator for Neopolitan

  • My plain-text document format

  • A simple, small, and extremely fast template engine for Rust

  • A powerful but minimal dependency template engine for Rust which is based on the syntax and behavior of the Jinja2 template engine for Python