Crates.io | handybars |
lib.rs | handybars |
version | 0.2.0 |
source | src |
created_at | 2023-05-22 17:17:22.666429 |
updated_at | 2023-05-22 17:17:22.666429 |
description | Minimal template parsing and expansion |
homepage | https://github.com/0x00002a/handybars |
repository | https://github.com/0x00002a/handybars |
max_upload_size | |
id | 870881 |
size | 62,773 |
This is a small library for template expansion. The syntax is based on
handlebars, but it only support expansion of variables. No #if
or #each
,
only {{ variable }}
. If you need actual handlebars support consider the
handlebars crate.
It has no dependencies and is designed to have a very simple API.
use handybars::{Context, Variable};
let ctx = Context::new().with_define("hello".parse().unwrap(), "world");
assert_eq!(ctx.render("hello {{ hello }}"), Ok("hello world".to_owned()));
You can also define objects
# use handybars::{Context, Variable, Object};
# let mut ctx = Context::new().with_define("hello".parse().unwrap(), "world");
ctx.define("obj".parse().unwrap(), Object::new().with_property("a", "value"));
assert_eq!(ctx.render("object a: {{ obj.a }}"), Ok("object a: value".to_owned()));
You can even have nested objects
# use handybars::{Context, Variable, Object};
# let mut ctx = Context::new().with_define("hello".parse().unwrap(), "world");
ctx.define("obj".parse().unwrap(), Object::new().with_property("b", Object::new().with_property("c", "value")));
assert_eq!(ctx.render("nested: {{ obj.b.c }}"), Ok("nested: value".to_owned()));
Note that objects cannot be directly expanded:
use handybars::{Context, Variable, Object, Error};
let ctx = Context::new().with_define("world".parse().unwrap(), Object::new().with_property("a", "p1"));
assert_eq!(ctx.render("{{world}}"), Err(Error::TriedToExpandObject(Variable::single("world"))));