A simple webpage
Returning a plain string is fine for tests, but most applications need real HTML. Garvan uses Crow's mustache engine for templating — let's render a page from disk.
Static page
Create public/fancypage.html:
<!DOCTYPE html>
<html>
<body>
<p>Hello World!</p>
</body>
</html>
Now tell mustache where to look for templates and load the file from a handler:
#include "vendors/Garvan/crow.h"
int main()
{
crow::SimpleApp app;
crow::mustache::set_global_base("public/");
CROW_ROUTE(app, "/")([] {
return crow::mustache::load_text("fancypage.html");
});
app.port(9090).multithreaded().run();
}
load_text reads the file without parsing any mustache tags, so it's the fastest option for purely static HTML.
Page with a variable
To inject data, switch from load_text to load, build a context, and render. Update the template:
<!DOCTYPE html>
<html>
<body>
<p>Hello !</p>
</body>
</html>
And the route:
CROW_ROUTE(app, "/<string>")([](std::string name) {
auto page = crow::mustache::load("fancypage.html");
crow::mustache::context ctx;
ctx["name"] = name;
return page.render(ctx);
});
Visiting /Ada will now show Hello Ada!. The context can hold strings, numbers, lists and even lambdas — see the templating guide for the full mustache feature set Crow implements.