just_template
A template engine for code generation.
Template syntax
Simple parameters — <<<key>>>
The most basic substitution. Replaced with the value set via
[Template::insert_param] or the [tmpl!] macro.
# use Template;
let mut t = from;
t.insert_param;
assert_eq!;
Implementation blocks
Syntax: >>>>>>>>>> name / @@@ >>> name ... @@@ <<<
Template sections that can be instantiated multiple times with different parameter sets. Each instantiation is called an "arm".
# use Template;
let mut t = from;
let arms = t.add_impl;
arms.push;
arms.push;
let out = t.expand.unwrap;
assert!;
assert!;
Display blocks
Syntax: ??? >>> name / ??? <<<
Conditionally included sections. Hidden by default; shown when a parameter with the same name exists in the parameter map.
# use Template;
// Without enabling, the block is omitted
let t = from;
assert_eq!;
// Enable by inserting a param with the block name
let mut t = from;
t.insert_param;
let out = t.expand.unwrap;
assert!;
Display blocks also work inside implementation blocks, and can be controlled per arm by including the block name in that arm's parameter map:
# use Template;
let mut t = from;
let arms = t.add_impl;
// Arm 1: no "extra" → display block hidden
arms.push;
// Arm 2: has "extra" → display block shown for this arm only
arms.push;
let out = t.expand.unwrap;
assert!;
assert!;
assert!;
assert!;
The tmpl! macro
The tmpl! proc macro provides a concise syntax for
setting both simple parameters and implementation blocks.
# use ;
let mut t = from;
# let param: i32 = 1; // dummy
tmpl!;
When the template variable is named tmpl, it can be omitted:
# use ;
let mut tmpl = from;
tmpl! ;
assert_eq!;
Installation
Add this to your Cargo.toml:
[]
= "0.2"
License
This project is dual-licensed under MIT and Apache 2.0. See the LICENSE file for details.