Dealing with C++ web views
- by Jeffrey
I'm working, as an hobby (before any one rage out of their mind, I'm just trying to study C++ regarding something I love: web. I'm not trying to reinvent your precious wheel, and I'm not trying to create the new web technology. I just have the time to go for it.), creating a web CGI C++ library. I'm at a pretty good point, but in the future I see one big problem: views.
I'm used to the great <body><?php echo "Hey!"; ?></body> embedded php, but there's no such thing in C++, so I'm wondering:
How would you deal with views? Would you create a simple find-replace-variable templating system and deal with thousands of partial views? For example:
View view;
view.load("header.html");
view.load("nav.html");
view.load("post_start.html");
for (int i = 0; i < 10; i++) {
std::map<std::string, std::string> post;
Post p(i);
post = p.get();
view.load(post_view.html, post); // p is passed as argument and every `{% varname %}` in the html will be replaced with its value inside the map
}
view.load(post_end.html);
view.load(footer);
Would you create a simple templating system? So that we can deal with this C++ code:
std::vector<std::map<std::string, std::string>> posts;
Posts p;
posts = p.getAll();
view.load(posts.html, posts);
and then this HTML/TPL:
<html>
...
<body>
<h2> Posts </h2>
{% for (i = 0; i < 10; i++): %}
<div class="post">...</div>
{% endfor %}
</body>
</html>
Is there any other way? What is the best way to do this? (And no, I don't think this is subjective question)