handybars

Crates.iohandybars
lib.rshandybars
version0.2.0
sourcesrc
created_at2023-05-22 17:17:22.666429
updated_at2023-05-22 17:17:22.666429
descriptionMinimal template parsing and expansion
homepagehttps://github.com/0x00002a/handybars
repositoryhttps://github.com/0x00002a/handybars
max_upload_size
id870881
size62,773
Natasha England-Elbro (0x00002a)

documentation

README

Handybars

GitHub Workflow Status GitHub

Introduction

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.

Usage

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"))));
Commit count: 62

cargo fmt