int cgiMain() { cgiHeaderContentType("text/html;charset=gbk"); int n, f; char txt1[32], txt2[32]; TMPL_varlist *mainlist, *vl; TMPL_loop *loop; TMPL_loop *persons; loop = 0; f = 1; for (n = 1; n < 11; n++) { sprintf(txt1, "%d", n); sprintf(txt2, "%d", f *= n); vl = TMPL_add_var(0, "n", txt1, "nfact", txt2, 0); loop = TMPL_add_varlist(loop, vl); } for (n = 10; n < 21; n++) { sprintf(txt1, "%d", n); sprintf(txt2, "%d", f *= n); vl = TMPL_add_var(0, "n1", txt1, "nfact1", txt2, "aa", "mmmm", 0); persons = TMPL_add_varlist(persons, vl); } mainlist = TMPL_add_var(0, "title", "10 factorials", 0); mainlist = TMPL_add_loop(mainlist, "fact", loop); mainlist = TMPL_add_loop(mainlist, "person", persons); TMPL_write("../resource/template/p1.html", 0, 0, mainlist, stdout, stderr); TMPL_free_varlist(mainlist); return 0; }
TMPL_varlist *wordzy_ctrl_play(mg_wordzy_ctrl *self, TMPL_varlist *vars) { mg_wordzy_game *wordzy_game = NULL; TMPL_loop *cards = NULL; TMPL_varlist *card = NULL; int i; char tmp[WC_TMP_LEN]; char tmp2[WC_TMP_LEN]; char tmp3[WC_TMP_LEN]; memset(tmp, WC_TMP_LEN, 0); memset(tmp2, WC_TMP_LEN, 0); memset(tmp3, WC_TMP_LEN, 0); vars = TMPL_add_var(vars, "rtfm-path", RTFM_PATH, NULL); wordzy_game = wordzy_game_alloc(NULL); wordzy_game_init(wordzy_game, NULL, 0); wordzy_repo_game_insert(self->wordzy_repo, wordzy_game); snprintf(tmp, WC_TMP_MAX, "%d", wordzy_game->game_id); vars = TMPL_add_var(vars, "game-id", tmp, NULL); snprintf(tmp, WC_TMP_MAX, "%d", wordzy_game_total_score(wordzy_game)); vars = TMPL_add_var(vars, "total-score", tmp, NULL); snprintf(tmp, WC_TMP_MAX, "%d", wordzy_game->discards_remaining); vars = TMPL_add_var(vars, "discards-remaining", tmp, NULL); for(i = 0; i < WORDZY_HAND_LENGTH; i++) { snprintf(tmp, WC_TMP_MAX, "%d", i); snprintf(tmp2, WC_TMP_MAX, "%c", wordzy_game->hand[i] - 32); snprintf(tmp3, WC_TMP_MAX, "%d", wordzy_game_letter_points(wordzy_game->hand[i])); card = TMPL_add_var(NULL, "card-id", tmp, "card-letter", tmp2, "card-points", tmp3, NULL); cards = TMPL_add_varlist(cards, card); } vars = TMPL_add_loop(vars, "hand-loop", cards); return vars; }
int main(int argc, char **argv, char **env) { int i; char num[32]; char *p; TMPL_fmtlist *fmt = 0; TMPL_varlist *vl = 0; TMPL_loop *loop; /* put command line arguments into a loop variable */ loop = 0; for (i = 0; i < argc; i++) { sprintf(num, "%d", i); loop = TMPL_add_varlist(loop, TMPL_add_var(0, "anum", num, "avalue", argv[i], 0)); } /* add loop variable to top level variable list */ vl = TMPL_add_loop(vl, "arg", loop); /* add a total to the loop variable */ sprintf(num, "%d", argc); TMPL_add_varlist(loop, TMPL_add_var(0, "total", num, 0)); /* put environment variable names and values into a loop variable */ loop = 0; for (i = 0; env[i] != 0; i++) { if ((p = strchr(env[i], '=')) != 0) { *p = 0; loop = TMPL_add_varlist(loop, TMPL_add_var(0, "ename", env[i], "evalue", p + 1, 0)); *p = '='; } } /* add a total to the loop variable */ sprintf(num, "%d", i); loop = TMPL_add_varlist(loop, TMPL_add_var(0, "total", num, 0)); /* add the loop variable to the top level variable list */ vl = TMPL_add_loop(vl, "env", loop); /* add another variable to the top level variable list */ vl = TMPL_add_var(vl, "title", "Environment Variables", 0); /* Build format function list */ fmt = TMPL_add_fmt(0, "trunc", truncate); TMPL_add_fmt(fmt, "entity", TMPL_encode_entity); /* output the template and free memory */ TMPL_write("printenv.tmpl", 0, fmt, vl, stdout, stderr); TMPL_free_varlist(vl); TMPL_free_fmtlist(fmt); return 0; }