3 unstable releases

Uses old Rust 2015

0.2.1 Nov 29, 2018
0.2.0 Nov 29, 2018
0.1.0 Nov 1, 2018

#313 in Template engine

Download history 909/week @ 2024-03-14 701/week @ 2024-03-21 829/week @ 2024-03-28 865/week @ 2024-04-04 716/week @ 2024-04-11 1078/week @ 2024-04-18 1375/week @ 2024-04-25 1625/week @ 2024-05-02 1092/week @ 2024-05-09 810/week @ 2024-05-16 1448/week @ 2024-05-23 1479/week @ 2024-05-30 1174/week @ 2024-06-06 1532/week @ 2024-06-13 1673/week @ 2024-06-20 1307/week @ 2024-06-27

5,940 downloads per month
Used in 10 crates (4 directly)

MIT/Apache

9KB
79 lines

string_template

Very simple string template for Rust. Can render named parameters from a HashMap or positional parameters from a slice.

Usage

Add this to your Cargo.toml:

[dependencies]
string_template = "0.2.1"

If you're on Rust 2015, and this to your crate root:

extern crate string_template;

Here's a simple example:

extern crate string_template;                                                             
                                                                                          
use string_template::Template;                                                            
use std::collections::HashMap;                                                            
                                                                                          
fn main() {                                                                               
    let template = Template::new("Hi, my name is {{name}} and I'm a {{lang}} developer.");
    let mut args = HashMap::new();                                                        
    args.insert("name", "Vader");                                                         
    args.insert("lang", "Dart");                                                          
    let s = template.render(&args);                                                       
                                                                                          
    assert_eq!(s, "Hi, my name is Vader and I'm a Dart developer.");                      
                                                                                          
    let template2 = Template::new("Hi, my name is {{}} and I'm a {{}} developer.");       
    let args2 = vec!["Vader", "Dart"];                                                    
    let s2 = template2.render_positional(&args2);                                         
                                                                                          
    assert_eq!(s2, "Hi, my name is Vader and I'm a Dart developer.");                     
}                                                                                         

Dependencies

~2.2–3MB
~53K SLoC