[][src]Crate gtmpl_helpers

The point in this Library is to provide useful helper methods for cases where you really do need the templates to be capable. My personal use case was to be able to generate specific svg layouts from numbers in card files. This meant doing more maths in the template than is common. I've broken up the helpers roughly by subject so it should be easy to find the ones you need.

The simplest way to use this is to import the trait and call with_all on a template.

use gtmpl::{Template,Context};
use gtmpl_value::{Value,Number};

use gtmpl_helpers::THelper;
let mut t = Template::default().with_all();
t.parse(r#"<rect {{xywh (mul . 5) (add . 11) 40 20 "px"}}/>"#);

let s = t.q_render(4).unwrap();
assert_eq!(s,r#"<rect x="20px" y="15px" width="40px" height="20px" />"#.to_string())

I wanted to keep the demo function simple, but q render works with anything that impls Into for Value And these can of course be Gtmpl Derived like most things. However that is tricky to demo in a doctest.



Executors for other programs Not included in all because of security issues. While there is a use case for including them, it these helpers should not be added lightly to your application, especially not to web facing systems.


Functions like add and mul


Functions that help with ranging is_list checks you have a list returning bool as_list creates a list from whatever you give it safe_len returns a length for lists maps and strings, else 0


Provides the selction uptions like b_sel (basically a turnary) and match


String functions like ccat (concat) and sep( Separate with )


Domain specific svg methods for creating trickier shapes.



This trait exists to give methods to the Template object directly. It is not intended to be applied to anything else. By "use"ing this trait, you gain the ability to write "with_svg()" or "with_all() and gain the appropriate helpers for your template q_render is also included to make it slightly easier to render a template. Rather than having to perform Context::from().unwrap() yourself