diff options
author | sternenseemann <0rpkxez4ksa01gb3typccl0i@systemli.org> | 2020-08-25 11:56:30 +0200 |
---|---|---|
committer | sternenseemann <0rpkxez4ksa01gb3typccl0i@systemli.org> | 2020-08-25 11:56:30 +0200 |
commit | edd4a7e4d5f4d5be6a5fc9db3f7173fc8dc81616 (patch) | |
tree | e30c66c6c7de62c555768fdad5798d849988975d /main.c | |
parent | 09809f4b19e25c6f479bb43fd1a50b407c07d815 (diff) |
feat(template): pass context to template, simplify api
New struct template_data is passed to all template functions, allowing to e. g. reflect the current entry in template_header and to unify template_single_entry, template_index_entry, template_error into template_main which switches behavior depending on data.page_type. BREAKING CHANGES: * remove template_single_entry, template_index_entry, template_error; replaced by template_main * template_header, template_footer: type change refactor(templates/simple): use xml_escape where applicable feat(templates/simple): navigate using header / entry titles feat(templates/simple): change <title> depending on subpage
Diffstat (limited to 'main.c')
-rw-r--r-- | main.c | 41 |
1 files changed, 31 insertions, 10 deletions
diff --git a/main.c b/main.c index 8571bdc..dfbfd5f 100644 --- a/main.c +++ b/main.c @@ -208,34 +208,55 @@ int main(void) { // confirm status and page_type match assert(status == 200 || page_type == PAGE_TYPE_ERROR); + assert(page_type != PAGE_TYPE_ERROR || status != 200); + + // initial contents of data, changed in loop for PAGE_TYPE_INDEX + struct template_data data; + data.page_type = page_type; + data.status = status; + data.script_name = script_name; + data.path_info = path_info; + + // confirm that we have SCRIPT_NAME and PATH_INFO unless an error occurred + assert(data.page_type != PAGE_TYPE_ERROR || + (data.path_info != NULL && data.script_name != NULL)); + + // make sure that PAGE_TYPE_ENTRY will have an entry set in template_header + if(count > 0) { + data.entry = entries; + } else { + data.entry = NULL; + } + assert(page_type != PAGE_TYPE_ENTRY || data.entry != NULL); + // render response if(page_type == PAGE_TYPE_ERROR) { send_standard_headers(status, "text/html"); - template_header(); - template_error(status); - template_footer(); + template_header(data); + template_main(data); + template_footer(data); } else if(is_feed == FEED_TYPE_NONE) { // either PAGE_TYPE_INDEX or PAGE_TYPE_ENTRY send_standard_headers(200, "text/html"); - template_header(); + template_header(data); // confirm that PAGE_TYPE_ENTRY → count == 1 assert(page_type != PAGE_TYPE_ENTRY || count == 1); for(int i = 0; i < count; i++) { if(entries[i].text != NULL || entry_get_text(&entries[i]) != -1) { - if(page_type == PAGE_TYPE_INDEX) { - template_index_entry(entries[i]); - } else { - template_single_entry(entries[i]); - } + data.entry = &entries[i]; + assert(data.entry != NULL); + + template_main(data); + entry_unget_text(&entries[i]); } } - template_footer(); + template_footer(data); } else if(is_feed == FEED_TYPE_RSS) { blog_rss(script_name, entries, count); } else if(is_feed == FEED_TYPE_ATOM) { |