aboutsummaryrefslogtreecommitdiff
path: root/just_template_macros/README.md
blob: a45d77d6ae1d4bec9ea2661810e616be8a80a256 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# just_template_macros

> Proc-macro crate for [`just_template`](https://crates.io/crates/just_template).
> Provides the `tmpl!` macro.

## `tmpl!` macro

A unified macro for setting both simple parameters and implementation blocks
on a [`Template`](https://docs.rs/just_template).

### Simple parameters

```rust
use just_template::{Template, tmpl};

let mut tmpl = Template::from("Hello, <<<name>>>!".to_string());

tmpl!(tmpl, name = "World");
// or, when the variable is named `tmpl`:
tmpl! { name = "World" };

assert_eq!(tmpl.expand().unwrap(), "Hello, World!");
```

### Implementation blocks with arms

```rust
use just_template::{Template, tmpl};

let mut tmpl = Template::from(r#"
>>>>>>>>>> arms
@@@ >>> arms
    <<<value>>> => println!("<<<value>>>"),
@@@ <<<
"#.trim().to_string());

tmpl! {
    arms {
        value = "a",
        value = "b",
    }
};

let out = tmpl.expand().unwrap();
// out contains:
//   a => println!("a"),
//   b => println!("b"),
```

### Mixed with multi-param arms

```rust
use just_template::{Template, tmpl};

let mut tmpl = Template::from(r#"
>>>>>>>>>> arms
@@@ >>> arms
    <<<name>>>: <<<value>>>
??? >>> extra
    <<<name>>>.extra()
??? <<<
@@@ <<<
"#.trim().to_string());

tmpl! {
    arms {
        name = "foo", value = "1",
        {
            name = "bar",
            value = "2",
            extra = ""  // enables the `extra` display block for this arm
        }
    }
};

let out = tmpl.expand().unwrap();
// out contains:
//   foo: 1
//   bar: 2
//   bar.extra()
```

### Call forms

| Form | Example | Template variable |
|---|---|---|
| Explicit | `tmpl!(tmpl, key = val)` | First argument |
| Implicit | `tmpl! { key = val }` | Defaults to `tmpl` |

Both forms accept the same body syntax: simple params (`key = val`),
implementation blocks (`name { arms }`), or a mix of both.

For more details, see the [`just_template` documentation](https://docs.rs/just_template).