예제 #1
0
파일: test.c 프로젝트: xinzou/cweb
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;
}
예제 #2
0
static int fill_with_content(char *content, char *layout, char *result)
{
	FILE *fp;
	char layout_path[255];

	fp = fopen(result, "w+");
	if (fp == NULL) {
		printf("Could not open file : %s\n", strerror(errno));
		return -1;
	}

	load_variables_from_cfg(config_site.var);
	//template_add_pages(mainlist, "blog");

	mainlist = TMPL_add_var(mainlist, "content", content, 0);
	sprintf(layout_path, "./sources/_layout/%s.layout", layout);
	TMPL_write(layout_path, 0, 0, mainlist, fp, stderr);
	TMPL_free_varlist(mainlist);
	mainlist = NULL;
	fclose(fp);

	return 0;
}
예제 #3
0
/*
 * /login/
 *
 * HTML is in templates/login.tmpl
 *
 * Display the login screen.
 */
static void login(void)
{
	int ret = 1;
	unsigned long long sid;
	TMPL_varlist *vl = NULL;

	if (qvars) {
		ret = check_auth();
		if (ret == 0) {
			sid = log_login();
			create_session(sid);
			printf("Location: //\r\n\r\n");
			return; /* Successful login */
		}
	}

	if (ret == -1)
		vl = add_html_var(vl, "logged_in", "no");
	if (ret == -2)
		vl = add_html_var(vl, "enabled", "no");

	send_template("templates/login.tmpl", vl, NULL);
	TMPL_free_varlist(vl);
}
예제 #4
0
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;
}