so://phie

ductape

Static template generator for C
git clone git://soophie.de/ductape

blob: 8a25b5f26b7990a060c8950f221bdf0a9f94fe5c (1032 bytes)[doc][blame][raw]

1# so/ctl

2

3Static template generator for C

4

5## Installation

6

7```

8sudo make install

9```

10

11## Usage

12

13```html

14% #include "macro.h" %

15

16% TEMPLDEF(

17 sample,

18 char *message;

19) %

20

21<h1>% STRING($(message)) %</h1>

22<ul>

23 % for (size_t i = 0; i < 10; i++) { %

24 <li>% NUMBER(i) %</li>

25 % } %

26</ul>

27

28% ENDTEMPL %

29```

30

31```c

32#define SO_STR_IMPL

33#include <so/str.h>

34

35#define CTL_OUT (str_t *) ctl->out

36

37#define CTL_WRITE($str) { \

38 str_append(CTL_OUT, $str); \

39}

40

41#define $($var) ctx->$var

42

43#define STRING CTL_WRITE

44

45#define NUMBER($num) { str_appendf(CTL_OUT, "%%d", $num); }

46```

47

48```c

49#include <stdio.h>

50

51#define SO_CTL_IMPL

52#include <so/ctl.h>

53#define SO_STR_IMPL

54#include <so/str.h>

55

56#include "../sample.h"

57

58int main(void) {

59 str_t out = STR_NULL;

60 ctl_t *ctl = ctl_init(&out);

61 sample_t data = {

62 .message = "It works!",

63 };

64 sample_render(ctl, &data, "./sample.so");

65 printf("%s", str_get(&out));

66 str_free(&out);

67 ctl_cleanup(ctl);

68 return 0;

69}

70```

71

72```bash

73ctl -h sample.html > sample.h

74ctl -o sample.html > sample.so

75```