new_string_template

Crates.ionew_string_template
lib.rsnew_string_template
version1.5.1
sourcesrc
created_at2021-05-28 17:17:50.7546
updated_at2024-04-06 19:04:32.404039
descriptionSimple Customizable String-Templating Library for Rust.
homepagehttps://github.com/hasezoey/new_string_template
repositoryhttps://github.com/hasezoey/new_string_template
max_upload_size
id403249
size26,936
hasezoey (hasezoey)

documentation

README

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.5"

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:

  • Rust install with rustfmt & clippy (nightly version of mentioned components), see fmt.sh and clippy.sh
Commit count: 93

cargo fmt