#template

string_template

Very simple string template for Rust

3 unstable releases

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

#45 in Template engine

Download history 13/week @ 2018-11-04 4/week @ 2018-11-11 3/week @ 2018-11-18 3/week @ 2018-11-25 19/week @ 2018-12-02 26/week @ 2018-12-09 4/week @ 2018-12-16 19/week @ 2018-12-23 15/week @ 2018-12-30 2/week @ 2019-01-06 20/week @ 2019-01-13

36 downloads per month

MIT/Apache

8KB
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

~2MB
~36K SLoC