#include <iostream>#include <fstream>#include <htmlbuilder.hpp>using namespace std;// 将字符串写入文件void write_file(string filename, string content) {ofstream ofs(filename);ofs << content;ofs.close();}int main() {HtmlElement root = {"html"};// 短元素用add_with()方法添加root.add((new HtmlElement{"head"})->add_with(new HtmlElement{"title", "My Page"})->add_with(new HtmlElement{"meta", "", {{"charset", "utf-8"}}, true}));HtmlElement* body = new HtmlElement{"body"};root.add(body);HtmlElement* div = new HtmlElement{"div"};body->add(div);div->configkws({{"id", "main"}, {"class", "container<>"}});div->set_attr("style", "background-color:red;");div->set_attrs({{"data-name", "value"}, {"data-value", "123"}, {"class", "test"}});div->configcnt("&<html><div>content内容&");cout << div->parent()->render() << endl;// 输出父元素此刻的html代码div->add(new HtmlElement{"h1", "cpphtmlbuilder"});// 添加列表HtmlElement* ul = new HtmlElement{"ul"};div->add(ul);for (int i = 0; i < 10; i++) {ul->add(new HtmlElement{"li", to_string(i)});}// 删除倒数第二个liul->remove_child(-2);// HtmlElement* tli = new HtmlElement{"li", "test"};// ul->add(tli);// ul->remove_child(tli);// ul->remove_all_children();div->add(new HtmlElement{"", "content内容,只要标签名为空即可"});auto result = root.render();cout << result << endl;// write_file("test.html", result);}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。