templating

This commit is contained in:
2025-08-21 21:15:21 +01:00
parent a7404fed3d
commit 217adf91e9
16 changed files with 58 additions and 10 deletions

View File

@@ -1,14 +1,17 @@
#define CROW_STATIC_DIRECTORY "../static"
#include "templating.hpp"
#include <crow.h>
Templating templating{"../templates"};
int main() {
crow::SimpleApp app;
CROW_ROUTE(app, "/")([] {
return "hello world";
CROW_ROUTE(app, "/")([]() {
return templating.render_template("index.html");
});
app.port(8080).multithreaded().run();
}
// note: replace mustache with jinja
}

16
src/templating.cpp Normal file
View File

@@ -0,0 +1,16 @@
#include "templating.hpp"
crow::response Templating::render_template(const std::string& template_name, const inja::json& data) {
try {
inja::Template template_obj = inja_env.parse_template(template_name);
std::string rendered = inja_env.render(template_obj, data);
return crow::response(rendered);
} catch (const inja::RenderError& e) {
return crow::response(500, e.what());
}
}
crow::response Templating::render_template(const std::string& template_name) {
return render_template(template_name, inja::json{});
}