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

6
.gitmodules vendored Normal file
View File

@@ -0,0 +1,6 @@
[submodule "thirdparty/inja"]
path = thirdparty/inja
url = https://github.com/pantor/inja
[submodule "thirdparty/nlohmann"]
path = thirdparty/nlohmann
url = https://github.com/nlohmann/json

18
include/templating.hpp Normal file
View File

@@ -0,0 +1,18 @@
#ifndef TEMPLATING_HPP
#define TEMPLATING_HPP
#include <inja/inja.hpp>
#include <crow.h>
#include <string>
class Templating {
public:
Templating(const std::string& template_dir) : inja_env{template_dir} {}
crow::response render_template(const std::string& template_name, const inja::json& data);
crow::response render_template(const std::string& template_name);
private:
inja::Environment inja_env;
};
#endif

View File

@@ -1,12 +1,12 @@
# Directories
INCLUDE_DIR = inc
INCLUDE_DIRS = include thirdparty/inja/include thirdparty/nlohmann/include
SRC_DIR = src
BUILD_DIR = build
# Compiler and linker settings
CXX = g++
LIBS =
CXXFLAGS = -std=c++17 -I $(INCLUDE_DIR)
CXXFLAGS = -std=c++17 $(foreach dir,$(INCLUDE_DIRS),-I$(dir))
# Source and object files
SRC = $(wildcard $(SRC_DIR)/*.cpp)

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{});
}

View File

@@ -13,8 +13,8 @@
<meta name="theme-color" content="#63de90" data-react-helmet="true">
<meta property="og:site_name" content="Alfieking.dev">
<meta property="og:url" content="https://alfieking.dev/">
<meta property="og:title" content="{{ self.title() }}">
<meta property="og:description" content="{{ self.description() }}">
<meta property="og:title" content="{% block og-title %}Home - Alfie's basement{% endblock %}">
<meta property="og:description" content="{% block og-description %}server backend survivor{% endblock %}">
<meta property="og:image" content="{% block og_image %}/static/content/general_images/icon.webp{% endblock %}">
{% block head %}
{% endblock %}

View File

@@ -2,12 +2,15 @@
{% block title %}Home - Alfie's basement{% endblock %}
{% block description %}server backend survivor{% endblock %}
{% block og-title %}Home - Alfie's basement{% endblock %}
{% block og-description %}server backend survivor{% endblock %}
{% block head %}
<link rel="stylesheet" href="/static/css/index.css">
{% endblock %}
{%block content %}
{% block content %}
<section>
<h1>A lil bit abt me</h1>
<p>

1
thirdparty/inja vendored Submodule

Submodule thirdparty/inja added at 593ff96024

1
thirdparty/nlohmann vendored Submodule

Submodule thirdparty/nlohmann added at 000db7a6a2