1 unstable release
0.1.0 | Dec 20, 2022 |
---|
#431 in Template engine
10KB
88 lines
simpletemplate
Template engine written entirely in rust.
usage
// main.rs
use simpletemplate::render;
use serde_json::{json};
fn main() {
let data = json!({
"name": ["Bob Belcher"],
});
let content = "{{ name }}";
let res = render(content, data);
println!("{}", res); //returns Bob Belcher
}
template usage
For variables, use {{ variable_name }}
.
For array indexing, use {{ array_name[index] }}
.
To iterate over an array:
{{ for loop_variable in loop_iterable }}
loop_body
{{ endfor }}
To access the index:
{{ for loop_variable in loop_iterable }}
{{ index }}
{{ endfor }}
If statements: if_body
is rendered if condition
is not null
, false
, or "false"
.
{{ if condition }}
if_body
{{ else }}
else_body
{{ endif }}
View src/main.rs
to see how to render from an HTML file.
lib.rs
:
Render html templates.
Examples
// variables
let data = json!({ "foo": "bar" });
let template = r"{{ foo }}";
let expected = "bar";
assert_eq!(render(template, data), expected);
// logic statements
let data = json!({ "foo": "bar" });
let template = r"{{ if foo }}{{ foo }}{{ else }}foo is not set{{ endif }}";
let expected = "bar";
assert_eq!(render(template, data), expected);
let data = json!({ "foo": null });
let template = r"{{ if foo }}{{ foo }}{{ else }}foo is not set{{ endif }}";
let expected = "foo is not set";
assert_eq!(render(template, data), expected);
// for loop
let data = json!({ "items": ["a", "b", "c"] });
let template = r"{{ for item in items }}{{ item }}{{ endfor }}";
let expected = "abc";
assert_eq!(render(template, data), expected);
let data = json!({ "items": [] });
let template = r"{{ for item in items }}{{ item }}{{ endfor }}";
let expected = "";
assert_eq!(render(template, data), expected);
// accessing by index
let data = json!({ "items": ["a", "b", "c"] });
let template = r"{{ items[0] }}";
let expected = "a";
assert_eq!(render(template, data), expected);
let data = json!({ "items": ["a", "b", "c"] });
let template = r"{{ items[1] }}";
let expected = "b";
assert_eq!(render(template, data), expected);
let data = json!({ "items": ["a", "b", "c"] });
let template = r"{{ items[2] }}";
let expected = "c";
assert_eq!(render(template, data), expected);
// combining logic
let data = json!({
"foo": "bar",
"items": ["a", "b", "c"],
"show_items": true,
"show_foo": false,
});
let template = r"{{ if show_items }}{{ for item in items }}{{ item }}{{ endfor }}{{ endif }}{{ if show_foo }}{{ foo }}{{ endif }}";
let expected = "abc";
assert_eq!(render(template, data), expected);
let data = json!({
"foo": "bar",
"items": ["a", "b", "c"],
"show_items": true,
"show_foo": false,
});
let template = r"{{ if show_items }}{{ for item in items }}{{ item }}{{ endfor }}{{ endif }}{{ if show_foo }}{{ foo }}{{ else }}foo is not set{{ endif }}";
let expected = "abcfoo is not set";
assert_eq!(render(template, data), expected);
// blank template
let data = json!({});
let template = "";
let expected = "";
assert_eq!(render(template, data), expected);
// invalid variable
let data = json!({ "foo": "bar" });
let template = "{{ baz }}";
let expected = "null";
assert_eq!(render(template, data), expected);
// invalid index
let data = json!({ "items": ["a", "b", "c"] });
let template = "{{ items[5] }}";
let expected = "null";
assert_eq!(render(template, data), expected);
// invalid statement
let data = json!({});
let template = "{{ if foo }}{{ endif }}";
let expected = "";
assert_eq!(render(template, data), expected);
Dependencies
~2.4–4MB
~70K SLoC