blob: 05e6056ad51d70097c34f2046ef495a5604cc726 (
plain)
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
|
Unja [![Build Status](https://img.shields.io/travis/dannyvankooten/unja/master)](https://travis-ci.org/dannyvankooten/unja)
[![License: MIT](https://img.shields.io/github/license/dannyvankooten/unja)](https://github.com/dannyvankooten/unja/blob/master/LICENSE)
==========
A template engine for C, inspired by Jinja and Liquid.
### Example
File `base.tmpl`:
```html+jinja
<html>
<head><title>{% block title %}Default title{% endblock %}</title></head>
<body>
{% block content %}
{% endblock %}
</body>
</html>
```
File `child.tmpl`:
```html+jinja
{% extends "base.html" %}
{% block title %}Users{% endblock %}
{% block content %}
<ul>
{% for user in users %}
<li><a href="{{ user.url }}">{{ user.username | lower }}</a></li>
{% endfor %}
</ul>
{% endblock %}
```
File: `example.c`
```c
int main() {
// parse all templates in the given directory
struct env *env = env_new("./");
// create a hashmap for storing template variables
struct hashmap *vars = hashmap_new();
hashmap_insert(vars, "name", "John Doe");
// execute the template
char *output = template(env, "child.tmpl", vars);
printf("%s", output);
// clean-up allocated memory
free(output);
hashmap_free(vars);
env_free(env);
}
```
### License
MIT
|