Minimal compile-time Rust template engine

Overview

boilerplate


boilerplate is a minimal compile-time Rust text template engine.

Quick Start

Add boilerplate to your project's Cargo.toml:

[dependencies]
boilerplate = "*"

Create a template in templates/my-template.txt:

Foo is {{self.n}}!

Define, instantiate, and render the template context:

#[derive(boilerplate::Display)]
struct MyTemplate {
  n: u32,
}
assert_eq!(MyTemplate { n: 10 }.to_string(), "Foo is 10!\n");

See the docs for more examples.

You might also like...
A Minimal Windows SDK.

Minimal Windows 10 SDK Installs only the necessary Windows 10 .lib files to save you having to download the full Visual Studio package. You can either

Minimal Substrate node configured for smart contracts via pallet-contracts.

substrate-contracts-node This repository contains Substrate's node-template configured to include Substrate's pallet-contracts ‒ a smart contract modu

A terminal-based daily task management tool with minimal overhead
A terminal-based daily task management tool with minimal overhead

Arenta A terminal-based daily task management tool with minimal overhead. Demo Features Complete CRUD support of daily tasks with intuitive syntax Vis

A holistic, minimal documentation portal for the Polkadot developers.

polkadot-sdk-docs A holistic, minimal documentation portal for the Polkadot developers. Master Tutorial The very, very rough plan that I have so far i

Simple template for building smart contract(Rust) and RPC Client(web3.js) on Solana (WIP) ⛏👷🚧⚠️
Simple template for building smart contract(Rust) and RPC Client(web3.js) on Solana (WIP) ⛏👷🚧⚠️

Solana BPF Boilerplate Simple template for building smart contract(Rust) and RPC Client(web3.js) on Solana This boilerplate provides the following. Si

This is a template to build secret contracts in Rust to run in Secret Network

Secret Contracts Starter Pack This is a template to build secret contracts in Rust to run in Secret Network. To understand the framework better, pleas

A template to build smart contracts in Rust to run inside a Cosmos SDK module on all chains that enable it.

CosmWasm Starter Pack This is a template to build smart contracts in Rust to run inside a Cosmos SDK module on all chains that enable it. To understan

🖨 Template for Rust applications & smart contracts @okp4.

Rust Template Template for Rust projects @okp4. Purpose & Philosophy This repository holds the template for building Rust projects with a consistent s

A template for your next Rust project.

Opinionated Rust Template A template for your next Rust project. To start coding run: cargo generate tomkarw/opinionated-rust-template Only requires j

Comments
  • Expose functionlike macro invocation

    Expose functionlike macro invocation

    This library is great! I really like the simplicity and power of directly using Rust syntax for control flow.

    I would like to use the templating with an inline string, and without needing to define a custom struct. Using the MyTemplateTxt example from your README, it would become something like this:

    fn my_template(foo: bool, bar: Result<String, Box<dyn std::error::Error>>) -> String {
      boilerplate!(r#"
    %% if foo {
    Foo was true!
    %% }
    %% match bar {
    %%   Ok(ok) => {
    Pretty good: {{ ok }}
    %%   }
    %%   Err(err) => {
    Not so great: {{ err }}
    %%   }
    %% }
    "#)
    }
    

    In fact, I independently developed a similar library to boilerplate, which uses exactly this approach, see example: https://github.com/reinerp/utemplate/blob/main/utemplate/examples/simple.rs. But I think I prefer your template syntax to mine!

    There's a few advantages to the functionlike macro approach:

    1. The user doesn't need to define a struct for each template they care about. Instead, variable reference from templates are picked up from context. This allows things like global constants const SOME_CONSTANT: &str as well.

    2. The template is more powerful, allowing other aspects of control flow such as early returns. For example:

      fn format_all(foos: &[Result<usize, Box<dyn std::error::Error>>]) -> Result<String, Box<dyn std::error::Error>> {
        boilerplate!(r#"
      %%  for foo in foos {
      %%    let f = foo?;  // Note early return here.
            The next value is {{f}}.
      %%  }
      "#)
      }  
      
    3. The procmacro implementation is simpler. In particular, you don't need to parse the struct type (so you don't need syn as a dependency), and you don't need to handle generics (which are often a pain).

    opened by reinerp 1
  • Allow setting filename explicitly

    Allow setting filename explicitly

    There should be a way to set the filename explicitly, instead of using the struct name.

    I'm thinking:

    #[derive(Boilerplate)]
    #[boilerplate(filename = "foo.html")]
    struct Foo {
    }
    
    opened by casey 0
  • Audit HTML Escaping

    Audit HTML Escaping

    boilerplate implements HTML escaping, which is security-critical functionality. Boilerplates HTML escaping functionality should be audited before it can be considered ready to use in production environments with untrusted input.

    opened by casey 0
Owner
Casey Rodarmor
😻A N A R C H O ー ネ コ バ ス 😻
Casey Rodarmor
Rust implementation of the PTHash perfect hash function for static compile-time generated hash tables

QuickPHF QuickPHF is a Rust implementation of the PTHash minimal perfect hash function algorithm. It consists of two crates: quickphf - runtime code f

Darko Trifunovski 11 Oct 20, 2023
Crib - Extensible, Minimal Template for Sway Projects

crib • Extensible, Minimal Template for Sway Projects. Developing First Time? New to rust? Install with: curl --proto '=https' --tlsv1.2 -sSf https://

velleity.eth 17 Jul 31, 2022
Extensible, Minimal Template for Sway Projects

crib • Extensible, Minimal Template for Sway Projects. Developing First Time? New to rust? Install with: curl --proto '=https' --tlsv1.2 -sSf https://

andreas 17 Jul 31, 2022
A minimal esp-hal application template for use with cargo-generate

{{ project-name }} A minimalist template for use with cargo-generate to create no_std applications targeting Espressif's line of SoCs and modules. At

esp-rs 35 Dec 29, 2022
A minimal template for building smart contracts with Cairo 1.0

Minimal Cairo 1.0 Template Built with auditless/cairo-template A minimal template for building smart contracts with Cairo 1.0 using the Quaireaux proj

Auditless 68 Apr 21, 2023
Minimal template for simulating contracts with arbiter.

arbiter-template Minimal template for simulating contracts with arbiter. Usage Clone this repository git clone https://github.com/primitivefinance/arb

Primitive 28 Aug 15, 2023
A blazing fast, type-safe template engine for Rust.

markup.rs A blazing fast, type-safe template engine for Rust. markup.rs is a template engine for Rust powered by procedural macros which parses the te

Utkarsh Kukreti 209 Dec 24, 2022
Minimal flashloan borrower contracts with an extensible rust sdk to abstract wrapping generic onchain calls (similar to multicall3) with flashloans.

flashloan-rs • Minimal flashloan borrower contracts with an extensible rust sdk. Getting Started Flashloan-rs is published to crates.io as flashloan-r

White Noise 71 Apr 15, 2023
Minimal Bitcoin wallet intended for teaching rust-bitcoin

Insanely minimal Bitcoin wallet intended for demonstration of Rust Bitcoin ecosystem Absolutely DO NOT use with mainnet funds!!! No privacy - address

Martin Habovštiak 4 May 5, 2023
Minimal implementation of the Mimblewimble protocol.

Grin Grin is an in-progress implementation of the Mimblewimble protocol. Many characteristics are still undefined but the following constitutes a firs

null 5k Dec 28, 2022