home
NOTE: Under Construction - I'm in the middle of upgrading my site and lots of stuff is kinda broken. Please forgive the mess.

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:

September 2023
Code
[dependencies]
sailfish = "0.8.0"
Code
<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>
Code
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());
}
═══ § ═══

Footnotes And References