macro sailfish-macros

Simple, small, and extremely fast template engine for Rust

30 releases

0.9.0 Aug 26, 2024
0.9.0-beta Jun 15, 2024
0.8.4-beta.2 May 27, 2024
0.8.4-beta.1 Feb 20, 2024
0.2.0 Jul 16, 2020

#474 in Template engine

Download history 841/week @ 2024-07-03 926/week @ 2024-07-10 1008/week @ 2024-07-17 959/week @ 2024-07-24 845/week @ 2024-07-31 836/week @ 2024-08-07 987/week @ 2024-08-14 1370/week @ 2024-08-21 1221/week @ 2024-08-28 1330/week @ 2024-09-04 1348/week @ 2024-09-11 1142/week @ 2024-09-18 1260/week @ 2024-09-25 1173/week @ 2024-10-02 1584/week @ 2024-10-09 1332/week @ 2024-10-16

5,602 downloads per month
Used in sailfish

MIT license

95KB
2.5K SLoC

SailFish

Simple, small, and extremely fast template engine for Rust

TestsVersiondependency statusRust 1.60License: MIT

User Guide | API Docs | Examples

✨ Features

  • Simple and intuitive syntax inspired by EJS
  • Include another template file inside template
  • Built-in filters
  • Minimal dependencies (<15 crates in total)
  • Extremely fast (See benchmarks)
  • Better error message
  • Syntax highlighting support (vscode, vim)
  • Works on Rust 1.60 or later

🐟 Example

Dependencies:

[dependencies]
sailfish = "0.9.0"

You can choose to use TemplateSimple to access fields directly:

Template file (templates/hello.stpl):

<html>
  <body>
    <% for msg in messages { %>
      <div><%= msg %></div>
    <% } %>
  </body>
</html>

Code:

use sailfish::TemplateSimple;

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

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

Or use the more powerful Template/TemplateMut/TemplateOnce:

Template file (templates/hello.stpl):

<html>
  <body>
    <% for msg in &self.messages { %>
      <div><%= msg %></div>
    <% } %>
    <div><%= self.say_hello() %></div>
  </body>
</html>

Code:

use sailfish::Template;

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

impl HelloTemplate {
    fn say_hello(&self) -> String {
        String::from("Hello!")
    }
}

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

You can find more examples in examples directory.

🐾 Roadmap

  • Template trait (RFC)
  • Template inheritance (block, partials, etc.)

👤 Author

🇯🇵 Ryohei Machida

🤝 Contributing

Contributions, issues and feature requests are welcome!

Since sailfish is an immature library, there are many planned features that is on a stage of RFC. Please leave a comment if you have an idea about its design!

Also I welcome any pull requests to improve sailfish! Find issues with Status: PR Welcome label, and let's create a new pull request!

Show your support

Give a ⭐️ if this project helped you!

📝 License

Copyright © 2020 Ryohei Machida.

This project is MIT licensed.


This README was generated with ❤️ by readme-md-generator

Dependencies

~0.4–8MB
~63K SLoC