Last active
January 26, 2016 23:11
-
-
Save lpereira/6694015 to your computer and use it in GitHub Desktop.
How to use sequences with lwan template
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <assert.h> | |
#include <sys/types.h> | |
#include <dirent.h> | |
#include "lwan-status.h" | |
#include "lwan-template.h" | |
#include "strbuf.h" | |
struct file_list { | |
const char *path; | |
struct { | |
lwan_tpl_generator_t generator; | |
const char *name; | |
} file_list; | |
}; | |
int dir_list_generator(coro_t *coro) | |
{ | |
DIR *dir; | |
struct dirent *ent; | |
struct file_list *fl = coro_get_data(coro); | |
dir = opendir(fl->path); | |
if (!dir) | |
return 0; | |
while ((ent = readdir(dir))) { | |
fl->file_list.name = ent->d_name; | |
coro_yield(coro, 1); | |
} | |
closedir(dir); | |
return 0; | |
} | |
int main(void) | |
{ | |
static const char *str_tpl = "Listing of {{path}}: " \ | |
"files={{#file_list}}\t{{file_list.name}}\t{{/file_list}}"; | |
static lwan_var_descriptor_t lst_item_desc[] = { | |
TPL_VAR_STR(struct file_list, files_list.name), | |
TPL_VAR_SENTINEL | |
}; | |
static lwan_var_descriptor_t main_desc[] = { | |
TPL_VAR_STR(struct file_list, path), | |
TPL_VAR_SEQUENCE(struct file_list, file_list, dir_list_generator, lst_item_desc), | |
TPL_VAR_SENTINEL | |
}; | |
lwan_tpl_t *tpl; | |
tpl = lwan_tpl_compile_string(str_tpl, main_desc); | |
if (!tpl) { | |
puts("could not compile template"); | |
return 1; | |
} | |
struct bla_t b; | |
b.str1 = "ola!"; | |
b.str2 = "mundo"; | |
b.files.path = "/tmp"; | |
strbuf_t *str = lwan_tpl_apply(tpl, &b); | |
if (!str) { | |
puts("could not apply template"); | |
return 1; | |
} | |
printf("applied template:\n\n------%s\n------\n", strbuf_get_buffer(str)); | |
lwan_tpl_free(tpl); | |
strbuf_free(str); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi lpereira,
Thanks for this, it was very helpful! However, it didn't compile as there were some very minor bugs and it was slightly outdated for your newer templating library. It also took me a second to understand what was happening with the file system, so I removed that and used a simple array of structs.
Lastly, my example is outputting integers rather than the strings I input. I don't know why that's happening?!? Hope it helps!