[go: up one dir, main page]

markup 0.4.0

A blazing fast, type-safe template engine for Rust.
Documentation
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
# markup.rs

> A blazing fast, type-safe template engine for Rust.

`markup.rs` is a template engine for Rust powered by procedural macros which
parses the template at compile time and generates optimal Rust code to render
the template at run time. The templates may embed Rust code which is type
checked by the Rust compiler enabling full type-safety.

## Quick Start

Add the `markup` crate to your dependencies:

```toml
[dependencies]
markup = "0.4.0"
```

Define your template using the `markup::define!` macro:

```rust
markup::define! {
    Hello<'a>(name: &'a str) {
        {markup::doctype()}
        html {
            head {
                title { "Hello " {name} }
            }
            body {
                #main.container {
                    {Greeting { name: "Everyone!" }}
                    br;
                    {Greeting { name: name }}
                }
            }
        }
    }
    Greeting<'a>(name: &'a str) {
        p.greeting {
            "Hello " {name} "!"
        }
    }
}

fn main() {
    println!("{}", Hello { name: "Ferris" });
}
```

Render your template by either:

1. Writing it to any instance of `std::io::Write`:

   ```rust
   write!(writer, "{}", Hello { name: "Ferris" });
   ```

2. Converting it to a string and using it however you like:

   ```rust
   let string = Hello { name: "Ferris" }.to_string();
   ```

Rendering the template produces (manually prettified):

```html
<!DOCTYPE html>
<html>
  <head>
    <title>Hello Ferris</title>
  </head>
  <body>
    <div id="main" class="container">
      <p class="greeting">Hello Everyone!</p>
      <br>
      <p class="greeting">Hello Ferris!</p>
    </div>
  </body>
</html>
```

## Note

Due to [a limitation in syn](https://github.com/dtolnay/syn/issues/515), the
crate this crate uses to parse templates, it is necessary to wrap identifiers
which precede an opening brace to be put in parentheses.

Example:

```rust
// Wrong
@if let Some(foo) = bar {
  ...
}
@match foo {
  ...
}

// Right
@if let Some(foo) = *&(bar) {
  ...
}
@match *&(foo) {
  ...
}
```

## Syntax

<!-- Syntax -->

You can define multiple templates in a `define!` block.

```rust
markup::define! {
    First {
      "First!"
    }
    Second {
      "Second!"
    }
}
```

```rust
println!("{}", First);
println!("{}", Second.to_string());
```

```html
First!
Second!
```

A template can have bare literal strings and arbitrary expressions in braces.

```rust
markup::define! {
    Hello {
        "Hello,"
        " "
        "world!\n"
        {1 + 2}
        {'π'}
        {format!("{}{}", 3, 4)}
        {if true { Some(5) } else { None }}
        {if false { Some(6) } else { None }}
    }
}
```
```rust
println!("{}", Hello {});
```
```html
Hello, world!
3π345
```

Elements can either have children inside `{}` or be a void tag, ending with `;`.

```rust
markup::define! {
    Hello {
        div {}
        br;
    }
}
```
```rust
println!("{}", Hello {});
```
```html
<div></div><br>
```

An id and multiple classes can be applied to an element using CSS like selectors.
The value after `#` and `.` can be either an identifier, a literal string, or an expression inside braces.

```rust
markup::define! {
    Hello {
        .foo {
            .bar {}
        }
        button#go.button."button-blue" {}
        button#"go-back".{1 + 2}.{2 + 3} {}
    }
}
```
```rust
println!("{}", Hello {});
```
```html
<div class="foo"><div class="bar"></div></div><button id="go" class="button button-blue"></button><button id="go-back" class="3 5"></button>
```

Attributes can either be normal or boolean (ends with ?).
The name can be an identifier, a literal string, or an expression inside braces.
Boolean attributes are printed without value if true and omitted if false.
The value can be an Option, where None values are omitted and Some are unwrapped.

```rust
markup::define! {
    Hello {
        div[
            a = 1,
            b = "2",
            c? = true,
            d? = false,
            "e-f" = 3,
            {"g".to_string() + "-h"} = 4,
            i = None::<i32>,
            j = Some(5)
        ] {}
        "\n"
        br[k = 6];
        "\n"
        input[type = "text"];
    }
}
```
```rust
println!("{}", Hello {});
```
```html
<div a="1" b="2" c e-f="3" g-h="4" j="5"></div>
<br k="6">
<input type="text">
```

An element can have zero or more children inside braces.

```rust
markup::define! {
    Hello {
        .foo[a = 1] {
            "One"
            {0 + 1}
        }
        div {
            "Two"
            {1 + 1}
        }
    }
}
```
```rust
println!("{}", Hello {});
```
```html
<div class="foo" a="1">One1</div><div>Two2</div>
```

Automatic HTML escaping can be disabled using the `markup::raw` function.
This function accepts any type implementing std::fmt::Display.

```rust
markup::define! {
    Hello {
        "<&\">"
        {markup::raw("<span></span>")}
    }
}
```
```rust
println!("{}", Hello {});
```
```html
&lt;&amp;&quot;&gt;<span></span>
```

A template can accept simple arguments as well as generic arguments with where clauses.

```rust
markup::define! {
    Hello(foo: u32, bar: u32, string: String) {
        div {
            {foo + bar}
            {string}
        }
    }
}
```
```rust
println!("{}", Hello { foo: 1, bar: 2, string: String::from("hello") });
```
```html
<div>3hello</div>
```

```rust
markup::define! {
    Hello<'a, T: std::fmt::Debug, U>(arg: T, arg2: U, str: &'a str) where U: std::fmt::Display {
        div {
            {format!("{:?}", arg)}
            {format!("{}", arg2)}
            {str}
        }
    }
}
```
```rust
println!("{}", Hello { arg: (1, 2), arg2: "arg2", str: "str" });
```
```html
<div>(1, 2)arg2str</div>
```

Other templates can be embedded by simply putting them in braces.

```rust
markup::define! {
    Add(a: u32, b: u32) {
        span { {a + b} }
    }
    Hello {
        {Add { a: 1, b: 2 }}
        {Add { a: 3, b: 4 }}
    }
}
```
```rust
println!("{}", Hello {});
```
```html
<span>3</span><span>7</span>
```

@if

```rust
markup::define! {
    Classify(value: i32) {
        {value}
        " is "
        @if *value < 0 {
            "negative"
        } else if *value == 0 {
            "zero"
        } else {
            "positive"
        }
        ".\n"
    }
    Main {
        {Classify { value: -42 }}
        " "
        {Classify { value: 0 }}
        " "
        {Classify { value: 42 }}
    }
}
```
```rust
println!("{}", Main {});
```
```html
-42 is negative.
 0 is zero.
 42 is positive.
```

@if let

```rust
markup::define! {
    Classify(value: Option<i32>) {
        @if let Some(0) = *(value) {
            "Some(ZERO)"
        } else if let Some(value) = *(value) {
            "Some(" {value} ")"
        } else {
            "None"
        }
        "\n"
    }
    Main {
        {Classify { value: None }}
        {Classify { value: Some(0) }}
        {Classify { value: Some(1) }}
    }
}
```
```rust
println!("{}", Main {});
```
```html
None
Some(ZERO)
Some(1)
```

@match

```rust
markup::define! {
    Classify(value: Option<i32>) {
        @match *(value) {
          Some(1) | Some(2) => {
            "1"
            " or 2"
          }
          Some(n) if n == 3 => {
            {n} {n}
          }
          Some(_) => {
            "Other"
          }
          None => {
            "None"
          }
        }
        "\n"
    }
    Main {
        {Classify { value: None }}
        {Classify { value: Some(0) }}
        {Classify { value: Some(1) }}
        {Classify { value: Some(2) }}
        {Classify { value: Some(3) }}
    }
}
```
```rust
println!("{}", Main {});
```
```html
None
Other
1 or 2
1 or 2
33
```

@for

```rust
markup::define! {
    Main {
        @for i in 1..5 {
            {i} " * 2 = " {i * 2} ";\n"
        }
    }
}
```
```rust
println!("{}", Main {});
```
```html
1 * 2 = 2;
2 * 2 = 4;
3 * 2 = 6;
4 * 2 = 8;
```

Curly braces also accept single statements and items and outputs it as-is in the generated code.

```rust
markup::define! {
    Main {
        {let x = 1;}
        {fn add1(x: i32) -> i32 {
            x + 1
        }}
        {add1(x)}
    }
}
```
```rust
println!("{}", Main {});
```
```html
2
```

<!-- /Syntax -->