나는 코드를 짧고 깨끗하게 유지하는 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>과 < 리> 등 지금과 같은 요소에 대한 부울 상태, 가장 좋은 방법은 스마트 솔루션이 처리하는 것입니다 이? - 앤디
편집 : 내 새로운 솔루션 : 추가 요소 클래스 "간단한": 나는 멤버 함수를 사용할 수 있습니다
#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