1 #ifndef __BIAWEBDOCITEMLIST__
2 #define __BIAWEBDOCITEMLIST__
9 #include <fmt/format.h>
10 #include <fmt/chrono.h>
11 #include "biawebstrings.hpp"
12 #include "biawebdocument.hpp"
14 // to implement a list of items (documents) with creation/modified date/time display
16 // to implement a single document item
19 // Just the required fields to build the item
25 DocListItem (std::string title, std::string url,
26 std::time_t ctime, std::time_t mtime ) {
27 this->title = escape_html (title);
33 std::time_t get_mtime() {
36 void set_mtime(std::time_t mtime) {
39 std::time_t get_ctime() {
42 void set_ctime(std::time_t ctime) {
46 std::string get_url() {
50 void set_url(std::string url) {
54 std::string get_title() {
57 void set_title(std::string title) {
58 this->title = escape_html (title);
61 // output to HTML vide the template
62 std::string to_html (std::string templatedir);
65 // output to HTML vide the template
66 std::string DocListItem::to_html (std::string templatedir) {
67 std::ifstream templ (templatedir + "/doclistitem.tpl.html");
68 std::string templstr ((std::istreambuf_iterator<char> (templ)),
69 (std::istreambuf_iterator<char> ()) );
72 std::string outputhtml = fmt::format (templstr, fmt::arg("url", this->url),
73 fmt::arg("doctitle", this->title),
74 fmt::arg("cdate", *std::localtime (&this->ctime)),
75 fmt::arg("mdate", *std::localtime (&this->mtime)));
80 // to implement a document list (or table)
84 std::list<DocListItem> items;
86 void set_title (std::string title) {
87 this->title = escape_html (title);
89 // add a document item
90 void add_document_item (DocListItem docitem) {
91 this->items.insert (this->items.cend(), docitem);
93 // render to HTML from a template
94 std::string to_html (std::string templatedir);
97 std::string DocList::to_html (std::string templatedir) {
98 std::ifstream templ (templatedir + "/doclist.tpl.html");
99 std::string templstr ( (std::istreambuf_iterator<char> (templ) ),
100 (std::istreambuf_iterator<char> ()));
104 std::string outputhtml = "";
105 // if the number of elements is non zero
106 if (this->items.size () != 0) {
107 std::string docitems = "";
108 for (DocListItem item : this->items)
109 docitems += item.to_html (templatedir);
111 outputhtml = fmt::format (templstr,
112 fmt::arg ("title", this->title),
113 fmt::arg ("docitems", docitems));