1 #ifndef __BIAWEBSIDEBAR__
2 #define __BIAWEBSIDEBAR__
7 #include <fmt/format.h>
8 #include "biawebutil.hpp"
9 #include "biawebtemplate.hpp"
11 // classes to describe the a list of (link) items and sidebar containers which form part of
14 // class to represent a generic link item which can contain a text and link or only
16 class GenericLinkItem {
18 // sidebar text and url
22 std::string get_item_text () {
23 return this->itemtext;
25 void set_item_text (std::string text) {
26 this->itemtext = escape_html (text);
28 std::string get_item_url () {
31 void set_item_url (std::string url) {
32 this->itemurl = escape_html (url);
34 // output to HTML using a template specified
35 std::string to_html (Template *t);
37 GenericLinkItem (std::string text = "", std::string url = "") {
38 this->itemtext = escape_html (text);
39 this->itemurl = escape_html (url);
43 std::string GenericLinkItem::to_html (Template *t) {
44 std::string html = "";
45 // if url is not empty it is a link item so load the sidebar link template
46 if (! this->itemurl.empty ()) {
47 if (!this->itemtext.empty ())
49 std::string tpl_linkitem_str = t->get_genericlinklistitem_tpl ();
50 html = fmt::format (tpl_linkitem_str,
51 fmt::arg ("itemurl", this->itemurl),
52 fmt::arg ("itemtext", this->itemtext));
55 // Non link item. Load the normal sidebar item template.
56 else if (! this->itemtext.empty ())
58 std::string tpl_item_str = t->get_genericlistitem_tpl ();
59 html = fmt::format (tpl_item_str, fmt::arg ("itemtext", this->itemtext) );
64 // Class to represent a sidebar, which contains one heading and a list of items
65 // either links or non-link items.
68 std::string sidebar_title;
69 std::list <GenericLinkItem> items;
72 void set_title (std::string title) {
73 this->sidebar_title = title;
76 void add_sidebar_item (GenericLinkItem item) {
77 this->items.insert (this->items.cend(), item);
80 // render the sidebar using the template specified
81 std::string to_html (Template *t) ;
84 // render the sidebar to HTML representation from the template.
85 std::string SideBar::to_html (Template *t) {
86 std::string sidetpl_str = t->get_sidebar_tpl ();
88 std::string listitems;
89 // first get the sidebar items and render them to HTML
90 for (GenericLinkItem item : this->items) {
91 listitems += item.to_html (t);
94 std::string html = "";
95 // if there are items, sidebar should be rendered otherwise not needed
96 if (items.size () > 0)
97 html = fmt::format (sidetpl_str,
98 fmt::arg ("title", this->sidebar_title),
99 fmt::arg ("items", listitems));