#runtime #string #template

new_string_template

Simple Customizable String-Templating Library for Rust

6 stable releases

1.4.0 Aug 30, 2022
1.3.1 May 26, 2022
1.3.0 Mar 5, 2022
1.2.0 Jan 24, 2022
1.0.0 May 28, 2021

#14 in Template engine

Download history 1135/week @ 2022-06-11 1031/week @ 2022-06-18 854/week @ 2022-06-25 789/week @ 2022-07-02 858/week @ 2022-07-09 775/week @ 2022-07-16 652/week @ 2022-07-23 677/week @ 2022-07-30 1307/week @ 2022-08-06 1241/week @ 2022-08-13 1000/week @ 2022-08-20 1084/week @ 2022-08-27 1562/week @ 2022-09-03 1157/week @ 2022-09-10 1210/week @ 2022-09-17 685/week @ 2022-09-24

4,735 downloads per month
Used in rust_nickname_generater

MIT license

24KB
335 lines

New String Template

Simple Customizable String-Templating Library for Rust.

This Library is inspired by string_template

Usage

Add this to your Cargo.toml (or use cargo-add):

[dependencies]
new_string_template = "1.4"

Example with 2 data points (with fail enabled):

use new_string_template::template::Template;
use std::collections::HashMap;

fn main() {
    let templ_str = "Something {data1} be {data2}, and { not here }";
    let templ = Template::new(templ_str);
    let data = {
        let mut map = HashMap::new();
        map.insert("data1", "should");
        map.insert("data2", "here");
        map
    };

    let rendered = templ.render(&data).expect("Expected Result to be Ok");
    assert_eq!("Something should be here, and { not here }", rendered);
}

Example with 1 data point (with fail disabled):

use new_string_template::template::Template;
use std::collections::HashMap;

fn main() {
    let templ_str = "Something {data1} be {data2}, and { not here }";
    let templ = Template::new(templ_str);
    let data = {
        let mut map = HashMap::new();
        map.insert("data1", "should");
        // map.insert("data2", "here");
        map
    };

    let rendered = templ.render_nofail(&data);
    assert_eq!("Something should be {data2}, and { not here }", rendered);
}

Example with Custom Regex:

use new_string_template::template::Template;
use std::collections::HashMap;
use regex::Regex;

fn main() {
    // The following regex requires at least one space between "{{" and "}}" and allows variables with spaces
    let custom_regex = Regex::new(r"(?mi)\{\{\s+([^\}]+)\s+\}\}").unwrap();
    let templ_str = "Something {{ data1 }} be {{ data2 }}, and {{ data 3 }}";
    let templ = Template::new(templ_str).with_regex(&custom_regex);
    let data = {
        let mut map = HashMap::new();
        map.insert("data1", "should");
        map.insert("data2", "here");
        map.insert("data 3", "here too");
        map
    };

    let rendered = templ.render_nofail(&data);
    assert_eq!("Something should be here, and here too", rendered);
}

Note: with the default regex, a template-variable can have spaces or none at all.

Working on this Project

This project requires:

  • NodeJS with yarn installed (when working on an main branch)
  • Rust install with rustfmt & clippy (nightly version of mentioned components), see fmt.sh and clippy.sh

Dependencies

~1–1.3MB
~38K SLoC