(medium) lightweight version of Jinja2 for C++
 
 
Go to file
Kroket Ltd 13eccc467e update README 2022-10-01 01:28:23 +03:00
cmake Improvements: 2022-10-01 00:49:22 +03:00
include/jinja2cpp Improvements: 2022-10-01 00:49:22 +03:00
src Bump deps: Switch to polymorphic value and latest robin_hood_hash(#220) 2021-12-12 23:20:15 +03:00
thirdparty/nonstd Improvements: 2022-10-01 00:49:22 +03:00
.clang-format Fix clang and MSVC 2017 build with C++17 enabled 2020-01-04 23:18:41 +02:00
.gitignore Bump deps: Switch to polymorphic value and latest robin_hood_hash(#220) 2021-12-12 23:20:15 +03:00
.gitmodules Improvements: 2022-10-01 00:49:22 +03:00
CMakeLists.txt Improvements: 2022-10-01 00:49:22 +03:00
LICENSE Initial commit 2018-04-20 12:16:39 +03:00
README.md update README 2022-10-01 01:28:23 +03:00
jinja2cppmedium.pc.in Improvements: 2022-10-01 00:49:22 +03:00

README.md

Jinja2CppMedium

A "medium-lightweight" version of jinja2cpp that features a better build system.

There is also Jinja2CppLight. However, that version supports less Jinja2 features.

Improvements

jinja2cpp has a rather convoluted and unnecessarily complex CMake build script.

Our improvements are:

  • A single, more straightforward CMakeLists.txt (100 lines)
  • Removed all git submodules - link against (system) libs instead:
    • RapidJSON
    • Boost (system, filesystem)
    • fmt
  • Some small header-only libraries are included in the repo as-is:
    • string-view-lite, optional-lite, variant-lite, expected-lite
  • Removed support for the nlohmann/json library (only support RapidJSON)
  • Removed support for the Conan build system (only support CMake)
  • Removed test suite and CI/CD definition(s)
  • Added support for ccache (compilation cache)
  • Removed documentation

Installation

install into /usr/local/lib and /usr/local/include:

cmake -Bbuild .
make -Cbuild -j4
sudo make -Cbuild install

Copy cmake/public/FindJinja2CppMedium.cmake to your project so you may do:

find_package(Jinja2CppMedium REQUIRED)
target_include_directories(your_app PRIVATE ${Jinja2CppMedium_INCLUDE_DIRS})
target_link_libraries(your_app ${Jinja2CppMedium_LIBRARIES})

Usage

#include <iostream>
#include <string>

#include "jinja2cpp/template.h"

using namespace jinja2;

int main(void) {
    std::string source = R"(
        {% if TrueVal %}
        Hello from Jinja template!
        {% endif %}
    )";

    Template tpl;
    tpl.Load(source);
    ValuesMap params = {
        {"TrueVal", true},
        {"FalseVal", true},
    };
    std::string result = tpl.RenderAsString(params).value();
    std::cout << result << std::endl;
    return 0;
}

RapidJSON:

#include <iostream>
#include <string>

#include "jinja2cpp/template.h"
#include <jinja2cpp/binding/rapid_json.h>

using namespace jinja2;

int main(void) {
    const char *json = R"(
    {
        "message": "Hello World from Parser!",
        "big_int": 100500100500100,
        "bool_true": true
    }
    )";

    rapidjson::Document doc;
    doc.Parse(json);

    std::string source = R"(
        {{ json.message }}
    )";

    Template tpl;
    tpl.Load(source);

    ValuesMap params = {
        {"json", Reflect(doc)},
    };

    std::string result = tpl.RenderAsString(params).value();
    std::cout << result << std::endl;
    return 0;
}

License

MPL 2.0