atom-engine 5.0.2

A component-oriented template engine built on Tera with props, slots, and provide/inject context
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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
# Atom Engine v5

[![Crates.io](https://img.shields.io/crates/v/atom-engine)](https://crates.io/crates/atom-engine)
[![Docs](https://docs.rs/atom-engine/badge.svg)](https://docs.rs/atom-engine)
[![License](https://img.shields.io/crates/l/atom-engine)](LICENSE)
[![Rust Version](https://img.shields.io/badge/rust-1.70%2B-blue.svg)](https://rust-lang.org)
[![Build Status](https://github.com/ateeq1999/atom-engine/actions/workflows/ci.yml/badge.svg)](https://github.com/ateeq1999/atom-engine/actions)
[![codecov](https://codecov.io/gh/atom-engine/atom-engine/branch/main/graph/badge.svg)](https://codecov.io/gh/atom-engine/atom-engine)

A high-performance, component-oriented template engine for Rust built on [Tera](https://keats.github.io/tera/docs/).

## Features

- **Built on Tera** - Leverages Tera's robust parsing, caching, and expression evaluation
- **Component System** - Register reusable components with props, slots, and validation
- **Provide/Inject Context** - React-style context for passing data to descendants
- **Stack System** - Push content and render stacks
- **50+ Built-in Filters** - String, collection, number, date, HTML, conditional filters
- **Helper Macros/Directives** - @map, @filter, @each, @reduce, @flatten, @partition
- **Layout Inheritance** - Full support for Tera's `extends`/`block` system
- **Macros** - Define reusable template fragments
- **Async Support** - Render templates asynchronously with tokio
- **Component Caching** - Cache rendered components for performance
- **Parallel Rendering** - Render multiple templates in parallel with Rayon
- **Memory Pool** - Optional memory pooling for allocations

## Installation

```toml
[dependencies]
atom-engine = "5"
```

### Optional Features

```toml
[dependencies]
atom-engine = { version = "5", features = ["parallel", "async", "pool-alloc"] }
```

| Feature | Description |
|---------|-------------|
| `parallel` | Enable parallel rendering with Rayon |
| `async` | Enable async template rendering with Tokio |
| `pool-alloc` | Enable memory pooling for allocations |

## Quick Start

```rust
use atom_engine::Atom;
use serde_json::json;

fn main() {
    let mut engine = Atom::new();
    
    engine.add_template("hello.html", "Hello, {{ name }}!").unwrap();
    
    let result = engine.render("hello.html", &json!({"name": "World"})).unwrap();
    println!("{}", result);  // Hello, World!
}
```

## Template Syntax

### Variables

```html
{{ user.name }}
{{ items[0] }}
{{ company.address.city }}
{{ items | length }}
```

### Comments

```html
{# This is a comment #}
```

### Filters

Filters transform values. Chain multiple filters:

```html
{{ name | upper | capitalize }}
{{ text | truncate(length=50, end="...") }}
{{ items | sort | reverse | first }}
```

### Control Flow

#### Conditionals

```html
{% if user.is_active %}
    Welcome, {{ user.name }}!
{% elif user.is_pending %}
    Please verify your email.
{% else %}
    <a href="/login">Login</a>
{% endif %}
```

#### Boolean Operators

```html
{% if user.is_active and user.is_verified %}
    Verified user
{% endif %}

{% if is_admin or is_editor %}
    Can edit content
{% endif %}

{% if not user.is_banned %}
    Not banned
{% endif %}
```

#### Comparison Operators

```html
{% if count > 10 %}
    Many items
{% endif %}

{% if status == "active" %}
    Active
{% endif %}

{% if price >= 100 %}
    Premium
{% endif %}
```

#### Loops

```html
{% for item in items %}
    <li>{{ item }}</li>
{% endfor %}

{% for item in items %}
    {{ loop.index }}: {{ item }}
{% endfor %}

{% for item in items %}
    {% if loop.first %}First: {% endif %}{{ item }}
    {% if loop.last %} (Last){% endif %}
{% endfor %}

{% for item in empty_array %}
    {{ item }}
{% empty %}
    No items found
{% endfor %}
```

### Template Inheritance

#### Base Template (base.html)

```html
<!DOCTYPE html>
<html>
<head>
    <title>{% block title %}Default Title{% endblock %}</title>
    {% block extra_head %}{% endblock %}
</head>
<body>
    <header>{% block header %}Header{% endblock %}</header>
    <main>{% block content %}{% endblock %}</main>
    <footer>{% block footer %}Footer{% endblock %}</footer>
</body>
</html>
```

#### Child Template

```html
{% extends "base.html" %}

{% block title %}{{ page_title }}{% endblock %}

{% block content %}
    <h1>{{ article.title }}</h1>
    <p>{{ article.content }}</p>
{% endblock %}
```

### Includes

```html
{% include "header.html" %}

{% includeIf "optional.html" %}

{% includeFirst ["primary.html", "fallback.html"] %}
```

### Macros

```html
{% macro button(text, type="primary") %}
<button class="btn btn-{{ type }}">{{ text }}</button>
{% endmacro %}

{% macro input(name, type="text", placeholder="") %}
<input type="{{ type }}" name="{{ name }}" placeholder="{{ placeholder }}">
{% endmacro %}

{{ button("Submit", type="success") }}
{{ input("email", type="email") }}
```

## Components

### Registering Components

```rust
engine.register_component(
    "button",
    r#"<button class="btn btn-{{ type }}">{{ text }}</button>"#
).unwrap();
```

### Component with Props

```rust
engine.register_component(
    "card",
    r#"<div class="card">
        <h3>{{ title }}</h3>
        <div class="content">{{ $slots.default() }}</div>
    </div>"#
).unwrap();
```

### Using Components

```html
{% call button(text="Click Me", type="primary") %}

{% call card(title="My Card") %}
    Card content here
{% endcall %}
```

### Props Validation

```rust
engine.register_component(
    "user-card",
    r#"{%-- atom: @props(name:String, age:Number, active:Boolean=true) --%}
    <div class="user">
        <h3>{{ name }}</h3>
        <p>Age: {{ age }}</p>
    </div>"#
).unwrap();
```

### Scoped Slots

```html
{# In component #}
{% for item in items %}
    {{ $slots.item(item=item) }}
{% endfor %}

{# Usage #}
{% call list(items=users) %}
    {% fill item(item=user) %}{{ user.name }}{% endfill %}
{% endcall %}
```

## Context (Provide/Inject)

### Providing Values

```rust
engine.provide("theme", json!("dark"));
engine.provide("user", json!({"name": "Alice", "role": "admin"}));
```

### Using in Templates

```html
{# Access provided values #}
Theme: {{ __context.theme }}
User: {{ __context.user.name }}
```

## Stack System

### Push to Stack

```html
{% call push(name="scripts", content="<script>alert('hi')</script>") %}
{% call push(name="scripts", content="<script>console.log('test')</script>") %}
```

### Prepend to Stack

```html
{% call prepend(name="header", content="<li>First</li>") %}
```

### Render Stack

```html
{{ "scripts" | stack }}
```

## Filters Reference

### String Filters

| Filter | Description | Example |
|--------|-------------|---------|
| `upper` | Uppercase | `{{ "hello" \| upper }}``HELLO` |
| `lower` | Lowercase | `{{ "HELLO" \| lower }}``hello` |
| `capitalize` | Capitalize first | `{{ "hello" \| capitalize }}``Hello` |
| `title` | Title case | `{{ "hello world" \| title }}``Hello World` |
| `camel_case` | camelCase | `{{ "hello_world" \| camel_case }}``helloWorld` |
| `pascal_case` | PascalCase | `{{ "hello_world" \| pascal_case }}``HelloWorld` |
| `snake_case` | snake_case | `{{ "helloWorld" \| snake_case }}``hello_world` |
| `kebab_case` | kebab-case | `{{ "helloWorld" \| kebab_case }}``hello-world` |
| `truncate(length, end?)` | Truncate | `{{ "Hello World" \| truncate(length=5) }}``Hello...` |
| `slugify` | URL slug | `{{ "Hello World!" \| slugify }}``hello-world` |
| `pluralize(n, singular?, plural?)` | Pluralize | `{{ 1 \| pluralize }}``` |
| `replace(old, new)` | Replace | `{{ "Hi" \| replace(old="Hi", new="Hello") }}``Hello` |
| `remove(string)` | Remove | `{{ "Hello" \| remove(string="l") }}``Heo` |
| `prepend(string)` | Prepend | `{{ "World" \| prepend(string="Hello ") }}``Hello World` |
| `append(string)` | Append | `{{ "Hello" \| append(string=" World") }}``Hello World` |
| `strip` | Trim whitespace | `{{ "  hi  " \| strip }}``hi` |
| `nl2br` | Newlines to `<br>` | `{{ "a\nb" \| nl2br \| safe }}``a<br>b` |
| `word_count` | Count words | `{{ "hello world" \| word_count }}``2` |
| `char_count` | Count chars | `{{ "hello" \| char_count }}``5` |
| `starts_with(prefix)` | Check prefix | `{{ "hello" \| starts_with(prefix="hel") }}``true` |
| `ends_with(suffix)` | Check suffix | `{{ "hello" \| ends_with(suffix="llo") }}``true` |
| `contains(substring)` | Check contains | `{{ "hello" \| contains(substring="ell") }}``true` |

### Collection Filters

| Filter | Description | Example |
|--------|-------------|---------|
| `first` | First element | `{{ [1,2,3] \| first }}``1` |
| `last` | Last element | `{{ [1,2,3] \| last }}``3` |
| `length` | Count elements | `{{ [1,2,3] \| length }}``3` |
| `reverse` | Reverse array | `{{ [1,2,3] \| reverse \| join(",") }}``3,2,1` |
| `sort` | Sort array | `{{ [3,1,2] \| sort \| join(",") }}``1,2,3` |
| `group_by(attribute)` | Group by field | See docs |
| `where(attribute, value?)` | Filter array | See docs |
| `pluck(attribute)` | Extract field | `{{ users \| pluck(attribute="name") }}` |
| `join(separator)` | Join array | `{{ ["a","b"] \| join("-") }}``a-b` |
| `slice(start, length)` | Array slice | `{{ [1,2,3,4] \| slice(start=1, length=2) }}``[2,3]` |
| `uniq` | Unique elements | `{{ [1,2,1] \| uniq }}``[1,2]` |
| `shuffle` | Random shuffle | `{{ [1,2,3] \| shuffle }}` |
| `map(prop, transform?)` | Transform elements | See helper directives |
| `filter(key, value, op?)` | Filter elements | See helper directives |
| `each(index?)` | Iterate with index | See helper directives |
| `reduce(initial, prop?)` | Reduce to value | See helper directives |
| `flatten` | Flatten nested arrays | `{{ [[1,2],[3]] \| flatten }}``[1,2,3]` |
| `partition(key)` | Split array | See helper directives |

### Number Filters

| Filter | Description | Example |
|--------|-------------|---------|
| `round(precision?)` | Round number | `{{ 3.7 \| round }}``4` |
| `abs` | Absolute value | `{{ -5 \| abs }}``5` |
| `format(format?)` | Format number | `{{ 1000000 \| format }}``1,000,000` |
| `min` | Minimum | `{{ [3,1,2] \| min }}``1` |
| `max` | Maximum | `{{ [3,1,2] \| max }}``3` |
| `sum` | Sum | `{{ [1,2,3] \| sum }}``6` |
| `avg` | Average | `{{ [1,2,3] \| avg }}``2` |
| `ceil` | Ceiling | `{{ 3.1 \| ceil }}``4` |
| `floor` | Floor | `{{ 3.9 \| floor }}``3` |

### Date Filters

| Filter | Description | Example |
|--------|-------------|---------|
| `date(format?)` | Format date | `{{ now() \| date }}``2024-01-15` |

### HTML Filters

| Filter | Description | Example |
|--------|-------------|---------|
| `escape_html` | Escape HTML | `{{ "<>" \| escape_html }}``&lt;&gt;` |
| `safe` | Mark as safe | `{{ "<b>" \| safe }}``<b>` |
| `strip_tags` | Remove HTML tags | `{{ "<p>hi</p>" \| strip_tags }}``hi` |

### Encoding Filters

| Filter | Description | Example |
|--------|-------------|---------|
| `json_decode` | Parse JSON | `{{ '{"a":1}' \| json_decode \| safe }}` |
| `urlescape` | URL encode | `{{ "a b" \| urlescape }}``a%20b` |
| `urlunescape` | URL decode | `{{ "a%20b" \| urlunescape }}``a b` |
| `base64_encode` | Base64 encode | `{{ "hi" \| base64_encode }}``aGk=` |
| `base64_decode` | Base64 decode | `{{ "aGk=" \| base64_decode }}``hi` |

### Conditional Filters

| Filter | Description | Example |
|--------|-------------|---------|
| `default(value)` | Default value | `{{ null \| default(value="x") }}``x` |
| `when(then, else)` | Ternary | `{{ true \| when(then="yes", else="no") }}``yes` |
| `coalesce(other)` | First non-null | `{{ null \| coalesce(other="x") }}``x` |
| `defined` | Check defined | See docs |
| `undefined` | Check undefined | See docs |
| `empty` | Check empty | `{{ [] \| empty }}``true` |
| `not_empty` | Check not empty | `{{ [1] \| not_empty }}``true` |

## Helper Directives

### @map - Transform Array Elements

```html
{# Extract property #}
{{ users | map(prop="name") | join(", ") }}

{# With transform #}
{{ users | map(prop="name", transform="upper") | join(", ") }}
```

### @filter - Filter Array Elements

```html
{# By value #}
{{ users | filter(key="active", value=true) | map(prop="name") }}

{# By operator #}
{{ users | filter(key="age", value=18, op="gte") }}
```

Operators: `eq`, `ne`, `gt`, `gte`, `lt`, `lte`, `contains`, `exists`

### @each - Iterate with Index

```html
{% for item in items | each(index=true) %}
    {{ item.index }}: {{ item.value }}
{% endfor %}
```

### @reduce - Reduce to Single Value

```html
{{ numbers | reduce(initial=0) }}
{{ users | reduce(initial=0, prop="age") }}
```

### @flatten - Flatten Nested Arrays

```html
{{ [[1,2], [3,4]] | flatten | join(", ") }}
{# Output: 1,2,3,4 #}
```

### @partition - Split Array

```html
{% set partitioned = users | partition(key="active") %}
Matched: {{ partitioned.matched | length }}
Rest: {{ partitioned.rest | length }}
```

## Global Functions

| Function | Description |
|----------|-------------|
| `dump(...)` | Debug output to stderr |
| `log(...)` | Log to stderr |
| `range(start?, end, step_by?)` | Generate range array |
| `now(utc?, timestamp?)` | Current datetime |
| `uuid()` | Generate UUID |
| `random(min, max)` | Random number |
| `choice(array)` | Random array element |
| `file_exists(path)` | Check if file exists |
| `env(key)` | Get environment variable |
| `md5(string)` | MD5 hash |
| `sha256(string)` | SHA256 hash |
| `repeat(content, count, separator?)` | Repeat string |
| `times(times, start?, step?)` | Generate sequence |
| `loop(from, to, step?, inclusive?)` | Loop range |
| `iterate(array, limit?, skip?)` | Iterate with metadata |
| `object(keys, values)` | Create object from arrays |
| `merge(array1, array2)` | Merge arrays |
| `chunk(array, size)` | Chunk array |
| `zip(arrays)` | Zip arrays |
| `compact(array)` | Remove nulls |

## Advanced Features

### Component Caching

```rust
engine.enable_component_cache(true);

// Render components (will be cached)
let result = engine.render("page.html", &ctx).unwrap();

// Check cache stats
println!("Cache size: {}", engine.component_cache_len());

// Clear cache
engine.clear_component_cache();
```

### Async Rendering

```rust
#[cfg(feature = "async")]
async fn render() {
    let engine = Atom::new();
    let result = engine.render_async("template.html", &json!({})).await.unwrap();
}
```

### Parallel Rendering

```rust
#[cfg(feature = "parallel")]
let results = engine.render_many(&[
    ("t1.html", &json!({})),
    ("t2.html", &json!({})),
    ("t3.html", &json!({})),
]).unwrap();
```

### Memory Pool

```rust
use atom_engine::{MemoryPool, StringPool, PooledString};

// Create a memory pool
let pool = MemoryPool::new(4096, 16);

// Create pooled strings
let s = pool.allocate("Hello");
```

## Examples

Run the examples:

```bash
cargo run --example basic
cargo run --example components
cargo run --example layouts
cargo run --example filters
cargo run --example comprehensive_test
```

## Testing

```bash
# Run all tests
cargo test

# Run with coverage
cargo test -- --nocapture

# Run specific test
cargo test test_filters
```

## License

MIT OR Apache-2.0