diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..496b6f1 --- /dev/null +++ b/.gitmodules @@ -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 diff --git a/include/templating.hpp b/include/templating.hpp new file mode 100644 index 0000000..344a620 --- /dev/null +++ b/include/templating.hpp @@ -0,0 +1,18 @@ +#ifndef TEMPLATING_HPP +#define TEMPLATING_HPP + +#include +#include +#include + +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 \ No newline at end of file diff --git a/makefile b/makefile index a65aaad..c9ba699 100644 --- a/makefile +++ b/makefile @@ -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) diff --git a/src/main.cpp b/src/main.cpp index d1fd9d0..247400f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,14 +1,17 @@ #define CROW_STATIC_DIRECTORY "../static" +#include "templating.hpp" #include + +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 \ No newline at end of file +} \ No newline at end of file diff --git a/src/templating.cpp b/src/templating.cpp new file mode 100644 index 0000000..80c2011 --- /dev/null +++ b/src/templating.cpp @@ -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{}); +} \ No newline at end of file diff --git a/templates/bases/base.jinja b/templates/bases/base.html similarity index 96% rename from templates/bases/base.jinja rename to templates/bases/base.html index 44f434b..1a00a45 100644 --- a/templates/bases/base.jinja +++ b/templates/bases/base.html @@ -13,8 +13,8 @@ - - + + {% block head %} {% endblock %} diff --git a/templates/bases/directory.jinja b/templates/bases/directory.html similarity index 100% rename from templates/bases/directory.jinja rename to templates/bases/directory.html diff --git a/templates/errors/400.jinja b/templates/errors/400.html similarity index 100% rename from templates/errors/400.jinja rename to templates/errors/400.html diff --git a/templates/errors/404.jinja b/templates/errors/404.html similarity index 100% rename from templates/errors/404.jinja rename to templates/errors/404.html diff --git a/templates/errors/500.jinja b/templates/errors/500.html similarity index 100% rename from templates/errors/500.jinja rename to templates/errors/500.html diff --git a/templates/index.jinja b/templates/index.html similarity index 98% rename from templates/index.jinja rename to templates/index.html index eae876b..b3f653a 100644 --- a/templates/index.jinja +++ b/templates/index.html @@ -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 %} {% endblock %} -{%block content %} +{% block content %}

A lil bit abt me

diff --git a/templates/pages/events/crittersmk.jinja b/templates/pages/events/crittersmk.html similarity index 100% rename from templates/pages/events/crittersmk.jinja rename to templates/pages/events/crittersmk.html diff --git a/templates/pages/events/paws-n-pistons.jinja b/templates/pages/events/paws-n-pistons.html similarity index 100% rename from templates/pages/events/paws-n-pistons.jinja rename to templates/pages/events/paws-n-pistons.html diff --git a/templates/pages/toaster.jinja b/templates/pages/toaster.html similarity index 100% rename from templates/pages/toaster.jinja rename to templates/pages/toaster.html diff --git a/thirdparty/inja b/thirdparty/inja new file mode 160000 index 0000000..593ff96 --- /dev/null +++ b/thirdparty/inja @@ -0,0 +1 @@ +Subproject commit 593ff96024c8acfe0aac777e4df8992e4b15b4a6 diff --git a/thirdparty/nlohmann b/thirdparty/nlohmann new file mode 160000 index 0000000..000db7a --- /dev/null +++ b/thirdparty/nlohmann @@ -0,0 +1 @@ +Subproject commit 000db7a6a263b199450f26c38a40fd8fdade82a9