templating
This commit is contained in:
13
src/main.cpp
13
src/main.cpp
@@ -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
16
src/templating.cpp
Normal 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{});
|
||||
}
|
||||
Reference in New Issue
Block a user