What is the best way to produce HTML output in C++? [duplicate]
Asked Answered
C

1

8

Possible Duplicate:
C++ HTML template framework, templatizing library, HTML generator library

I have a program, that contains many tables, so I want to track them in log files when I debug the program. I want colored, formatted output so I thought I wrote it in HTML format. So what is the best way to create HTML files in C++?

The trivial way is so disgusting and error prone:

std::stringstream ret;
ret << "<TABLE BORDER=\"1\" CELLBORDER=\"0\">\n";
ret << "    <TR>\n";
...

So I thought I wrote a HTML wrapper but I think there are so many for this reason.

I want a statically typed HTML wrapper in C++ with this syntax or similar:

CHtmlHtml Html;
Html <<
    Body() % Id("1")
        << H1("My First Heading") <<
        << P("My first paragraph.");

or in this case

CHtmlTable table;
table % Border(1) % CellBorder(0) <<
    Tr() <<
        Td("Text") % Colspan(4);

Is there any similar project?

Ciapas answered 2/1, 2012 at 0:53 Comment(6)
@tzaman: I want a statically typed HTML wrapper, not a template based oneCiapas
I am unclear on your requirements that rule out templates, but the answer tzaman gave lists a whole bunch of useful solutions for C++, some not templates at all.Deedradeeds
I checked the offered link and could not see anything even close to what Industrial-antidepressant is talking about. They pretty much all come from HTML/XML templates.Pham
@AlexisWilke My apologies - I thought he meant C++ templates. Oops.Deedradeeds
Please reopen, I dont want HTML framework, templatizing library or HTML generator library. I want a domain specific language for HTML. So I want compile time errors when something is wrong.Ciapas
Who closed this question using my name as one of the person who said it should be closed?! I said the OPPOSITE. This is NOT the same and the other answer does NOT APPLY.Pham
L
4

HTML and XML both use the same syntax, so you could just use a C++ XML library (such as TinyXML) to dynamically create all the nodes you need.

This page shows how to use TinyXML to build XML (or in your case, HTML) documents.

You may need to do some fiddling (such as removing the XML declaration at the top of the generated file), but I think you could create a wrapper for it somewhat easily.

Lachellelaches answered 2/1, 2012 at 17:16 Comment(2)
yes, I can use XML for a base for my wrapper, but I didnt want to write a wrapper at all. :) But looks like this wrapper library does not exists, so I will write one...Ciapas
github.com/leethomason/tinyxml2Ethelred

© 2022 - 2024 — McMap. All rights reserved.