tpl_doc *tpl_ctx_get_tpl_doc(tpl_ctx *t, const char *inpath) { char tpath[MAXPATHLEN] = { 0 }; int err; tpl_doc *tdoc = NULL; tpl_doc *ndoc; tpl_doc *mdoc; const char *ext = NULL; char pext[MAXPATHLEN] = { 0 }; const char *next = NULL; int off = 0; ext = getext(inpath); if(ext == NULL) { seterrmsg(t, "File path has no extensions"); return NULL; } do { if(next != NULL) { if(strcmp(next, pext) != 0) off = 0; ext = next; } err = get_tpl_path(t, ext, tpath, &off); if(err) { if(tdoc != NULL) return tdoc; seterrmsg(t, "Did not find template for file extension '%s'", ext); return NULL; } ndoc = tpl_doc_parse(t, tpath); if(ndoc == NULL) { seterrmsg(t, "Failed to parse template file '%s': %s", tpath, tpl_ctx_error(t)); return NULL; } if(tdoc == NULL) tdoc = ndoc; else { mdoc = tpl_doc_merge(ndoc, tdoc); strncpy(pext, ext, sizeof(pext)); tpl_doc_destroy(tdoc); tpl_doc_destroy(ndoc); tdoc = mdoc; } } while((next = tpl_doc_get_definition(tdoc, "-template")) != NULL); return tdoc; }
char * read_tpl( struct zxy_request_context *context, const char *tpl_name ) { char *tpl_cache_path = get_tpl_cache_path(context, tpl_name); char *tpl_content; if ( access(tpl_cache_path, F_OK) != -1 ) { tpl_content = read_file(tpl_cache_path); } else { char *tpl_path = get_tpl_path(context, tpl_name); tpl_content = read_file(tpl_path); free_tpl_path(tpl_path); } free_tpl_cache_path(tpl_cache_path); return tpl_content; }