1 #ifndef __BIAWEBSIDEBAR__
2 #define __BIAWEBSIDEBAR__
7 #include <fmt/format.h>
8 #include "biawebutil.hpp"
10 // classes to describe the a list of items and sidebar item containers which form part of
13 // class to represent a sidebar item which can contain a text and link or only
17 // sidebar text and url
18 std::string sidebar_text;
19 std::string sidebar_url;
21 std::string get_sidebar_text () {
22 return this->sidebar_text;
24 void set_sidebar_text (std::string text) {
25 this->sidebar_text = escape_html (text);
27 std::string get_sidebar_url () {
28 return this->sidebar_url;
30 void set_sidebar_url (std::string url) {
31 this->sidebar_url = escape_html (url);
33 // output to HTML using a template directory specified
34 std::string to_html (std::string templatedir);
36 SideBarItem (std::string text = "", std::string url = "") {
37 this->sidebar_text = escape_html (text);
38 this->sidebar_url = escape_html (url);
42 std::string SideBarItem::to_html (std::string templatedir) {
44 // if url is not empty it is a link item so load the sidebar link template
45 if (! this->sidebar_url.empty ()) {
46 if (!this->sidebar_text.empty ())
48 std::ifstream tpl_linkitem (templatedir + "/sidebarlinkitem.tpl.html");
49 std::string tpl_linkitem_str ( (std::istreambuf_iterator<char> (tpl_linkitem)),
50 (std::istreambuf_iterator<char> ()));
51 tpl_linkitem.close ();
52 html = fmt::format (tpl_linkitem_str,
53 fmt::arg ("itemurl", this->sidebar_url),
54 fmt::arg ("itemtext", this->sidebar_text));
56 // no text or url - item is empty - so it should be blank
60 // Non link item. Load the normal sidebar item template.
63 std::ifstream tpl_item (templatedir + "/sidebaritem.tpl.html");
64 std::string tpl_item_str ( (std::istreambuf_iterator<char> (tpl_item)),
65 (std::istreambuf_iterator<char> ()));
67 html = fmt::format (tpl_item_str, fmt::arg ("itemtext", this->sidebar_text) );
72 // Class to represent a sidebar, which contains one heading and a list of items
73 // either links or non-link items.
76 std::string sidebar_title;
77 std::list <SideBarItem> items;
80 void set_title (std::string title) {
81 this->sidebar_title = title;
84 void add_sidebar_item (SideBarItem item) {
85 this->items.insert (this->items.cend(), item);
88 // render the sidebar using the template directory specified
89 std::string to_html (std::string templatedir) ;
92 // render the sidebar to HTML representation from the template.
93 std::string SideBar::to_html (std::string templatedir) {
94 std::ifstream sidetpl (templatedir + "/sidebar.tpl.html");
95 std::string sidetpl_str ( ( std::istreambuf_iterator<char> (sidetpl)) ,
96 (std::istreambuf_iterator<char> ()));
98 std::string listitems;
99 // first get the sidebar items and render them to HTML
100 for (SideBarItem item : this->items) {
101 listitems += item.to_html (templatedir);
104 std::string html = "";
105 // if there are items, sidebar should be rendered otherwise not needed
106 if (items.size () > 0)
107 html = fmt::format (sidetpl_str,
108 fmt::arg ("title", this->sidebar_title),
109 fmt::arg ("items", listitems));