I'm starting with cgicc which let me keep code short and clean. But now I'm struggling. I want to have something like that:
<ul>
<li>
<a href="#" ><span>Main 1</span></a>
<ul>
<li><a href="ref1-1" ><span>Sub 1</span></a></li>
<li><a href="ref1-2" ><span>Sub 2</span></a></li>
<li><a href="ref1-3" ><span>Sub 3</span></a></li>
</ul>
</li>
<li>
<a href="#" ><span>Main 2</span></a>
<ul>
<li><a href="ref2-1" ><span>Sub 1</span></a></li>
<li><a href="ref2-2" ><span>Sub 2</span></a></li>
<li><a href="ref2-3" ><span>Sub 3</span></a></li>
</ul>
</li>
</ul>
The inner blocks are very large, therefore I don't want to combine the calls. My first atttempt like this (I hope I coded it well):
cout << ul() << endl;
cout << li() << endl;
cout << a("Main 1").set("href", "#") << endl;
cout << ul() << endl; // <-- must fail here!
cout << li(a("Sub 1").set("href", "ref1-1")) << endl;
cout << li(a("Sub 2").set("href", "ref1-2")) << endl;
cout << li(a("Sub 3").set("href", "ref1-3")) << endl;
cout << ul() << endl;
cout << li() << endl;
cout << li() << endl;
cout << a("Main 2").set("href", "#") << endl;
cout << ul() << endl;
cout << li(a("Sub 1").set("href", "ref2-1")) << endl;
cout << li(a("Sub 2").set("href", "ref2-2")) << endl;
cout << li(a("Sub 3").set("href", "ref2-3")) << endl;
cout << ul() << endl;
cout << li() << endl;
cout << ul() << endl;
Problem is the boolean state for elements like < ul > and < li > etc. So, is there a best practice and smart solution to handle this? - Andi
EDIT: My new solution: additional elements class "simple":
#include <cgicc/HTMLElement.h>
#include <cgicc/HTMLAttributeList.h>
template<class Tag>
class HTMLSimpleElement : public cgicc::HTMLElement
{
public:
HTMLSimpleElement() : HTMLElement(0, 0, 0, EElementType(-1))
{}
HTMLSimpleElement(const cgicc::HTMLAttributeList& attributes) : HTMLElement(&attributes, 0, 0, EElementType(-1))
{}
virtual ~HTMLSimpleElement() {}
virtual inline HTMLElement* clone() const
{ return new HTMLSimpleElement<Tag>(*this); }
virtual inline const char* getName() const
{ return Tag::getName(); }
virtual void render(std::ostream& out) const
{
const cgicc::HTMLAttributeList* attributes = getAttributes();
out << '<' << getName();
if (attributes != NULL)
{
out << ' ';
attributes->render(out);
}
out << ">";
}
};
#define TAG(name, tag) \
class name##Tag \
{ public: inline static const char* getName() { return tag; } }
#define SIMPLE_ELEMENT(name, tag) \
TAG(name, tag); typedef HTMLSimpleElement<name##Tag> name
SIMPLE_ELEMENT (divB, "div");
SIMPLE_ELEMENT (divE, "/div");
// and so on...
This way I can use the member functions and have full control over the BEGIN and END tag. Anybody another, smarter solution?