我以cgicc開頭,讓我保持代碼簡短和乾淨。但現在我正在掙扎。我想有這樣的事情:cgicc:多層次的相同元素
<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>
內部塊是非常大的,所以我不想在呼叫組合。我喜歡這首atttempt(我希望我編寫得很好):
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;
問題是對於像< ul>和< LI>等,所以元素的布爾狀態,是有一個最佳實踐和智能解決方案來處理這個? - 安迪
編輯:我的新的解決方案:附加要素類的「簡單」:
#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...
這樣我可以使用成員函數,並已超過BEGIN和END標籤完全控制。任何人,更聰明的解決方案?
我可以提供一個解決方法:定義我自己的BOOLEAN_ELEMENT,如:BOOLEAN_ELEMENT(ul1,「ul」); BOOLEAN_ELEMENT(ul2,「ul」);等等。然後我使用關聯關卡上的每個具體編號。任何其他想法? – Andi