The words Under construction in black text on a yellow background with diagonal black stipes surrounding it
I'm in the process of moving my site. It's still a work in progress. Please excuse the mess and broken links.

Use Rust Struct Functions In A Sailfish Template

TODO: Pull subtitle into page object
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());
}

Notes

Debugging Stuff

I'm moving stuff around right now. All this below is helping me figure out where to put stuff

        -- title

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:

-- code
-- title: Cargo.toml

[dependencies]
sailfish = "0.8.0"

-- code
-- title: 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>


-- code
-- title: 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());
}


-- notes

- I really don't need the build to go any faster. 
The first build goes in 2 seconds and individual
page updates after that are virtually instant. This
is really more about experimenting with things. I'm
hoping the overall experience turns out to be
a little easier than working with Minijinja

- I tried this without the child struct. It worked
if there were not methods added via `impl`` but
as soon as I added one there it would throw
errors about things getting borrowed.


- I don't see any methods for calling functions 
listed in the docs

- The docs do mention evaluating Rust expressions

- Seems that sending things in like this works
fine


-- ref
-- title: Neopoligin
-- url: /neopoligin/

My static site generator for Neopolitan

-- ref
-- title: Neopolitan 
-- url: /neopolitan/

My plain-text document format

-- ref
-- title: Sailfish Template Engine 
-- url: https://rust-sailfish.github.io/sailfish/

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


-- ref
-- title: Minijinja
-- url: https://docs.rs/minijinja/latest/minijinja/

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


-- categories
-- Rust 
-- Sailfish 

-- metadata
-- date: 2023-09-18 15:56:51
-- id: 2vagw8oi
-- site: aws
-- type: post
-- status: published