C++ implementation of the Jinja2 Python template engine. This library brings support of powerful Jinja2 template features into the C++ world, reports dynamic HTML pages and source code generation.
Main features of Jinja2C++:
set
, for
, if
, filter
, do
, with
).For instance, this simple code:
#include <jinja2cpp/template.h>
std::string source = R"(
{{ ("Hello", 'world') | join }}!!!
{{ ("Hello", 'world') | join(', ') }}!!!
{{ ("Hello", 'world') | join(d = '; ') }}!!!
{{ ("Hello", 'world') | join(d = '; ') | lower }}!!!
)";
Template tpl;
tpl.Load(source);
std::string result = tpl.RenderAsString({}).value();
produces the result string:
Helloworld!!!
Hello, world!!!
Hello; world!!!
hello; world!!!
To use Jinja2C++ in your project you have to:
Usage of Jinja2C++ in the code is pretty simple:
jinja2::Template tpl;
tpl.Load("{{ 'Hello World' }}!!!");
std::cout << tpl.RenderAsString({}).value() << std::endl;
and get:
Hello World!!!
That's all!
More detailed examples and features description can be found in the documentation: https://jinja2cpp.github.io/docs/usage
Currently, Jinja2C++ supports the limited number of Jinja2 features. By the way, Jinja2C++ is planned to be a fully jinja2 specification-conformant. The current support is limited to:
Full information about Jinja2 specification support and compatibility table can be found here: https://jinja2cpp.github.io/docs/j2_compatibility.html.
Compilation of Jinja2C++ tested on the following compilers (with C++14 and C++17 enabled features):
Note: Support of gcc version >= 9.x or clang version >= 8.0 depends on the version of the Boost library provided.
Jinja2C++ has several external dependencies:
boost
library (at least version 1.65)nonstd::expected-lite
https://github.com/martinmoene/expected-litenonstd::variant-lite
https://github.com/martinmoene/variant-litenonstd::optional-lite
https://github.com/martinmoene/optional-litenonstd::string-view-lite
https://github.com/martinmoene/string-view-litefmtlib::fmt
https://github.com/fmtlib/fmtExamples of build scripts and different build configurations could be found here: https://github.com/jinja2cpp/examples-build
In simplest case to compile Jinja2C++ you need:
> git clone https://github.com/flexferrum/Jinja2Cpp.git
> cd Jinja2Cpp
> mkdir build
> cd build
> cmake .. -DCMAKE_INSTALL_PREFIX=<path to install folder>
> cmake --build . --target all
"Path to install folder" here is a path to the folder where you want to install Jinja2C++ lib.
> cmake --build . --target install
In this case, Jinja2C++ will be built with internally-shipped dependencies and install them respectively. But Jinja2C++ supports builds with externally-provided deps.
Jinja2C++ can be used as conan.io package. In this case, you should do the following steps:
jinja2cpp/1.2.1
) to your conanfile.txt, conanfile.py or CMakeLists.txt. For instance, with the usage of conan-cmake
integration it could be written this way:
cmake_minimum_required(VERSION 3.24)
project(Jinja2CppSampleConan CXX)
list(APPEND CMAKE_MODULE_PATH ${CMAKE_BINARY_DIR})
list(APPEND CMAKE_PREFIX_PATH ${CMAKE_BINARY_DIR})
add_definitions("-std=c++14")
if(NOT EXISTS "${CMAKE_BINARY_DIR}/conan.cmake")
message(STATUS "Downloading conan.cmake from https://github.com/conan-io/cmake-conan")
file(DOWNLOAD "https://raw.githubusercontent.com/conan-io/cmake-conan/0.18.1/conan.cmake"
"${CMAKE_BINARY_DIR}/conan.cmake"
TLS_VERIFY ON)
endif()
include(${CMAKE_BINARY_DIR}/conan.cmake)
conan_cmake_autodetect(settings)
conan_cmake_run(REQUIRES
jinja2cpp/1.1.0
gtest/1.14.0
BASIC_SETUP
${CONAN_SETTINGS}
OPTIONS
jinja2cpp/*:shared=False
gtest/*:shared=False
BUILD missing)
set (TARGET_NAME jinja2cpp_build_test)
add_executable (${TARGET_NAME} main.cpp)
target_link_libraries (${TARGET_NAME} ${CONAN_LIBS})
set_target_properties (${TARGET_NAME} PROPERTIES
CXX_STANDARD 14
CXX_STANDARD_REQUIRED ON)
You can define (via -D command-line CMake option) the following build flags:
internal
In this mode Jinja2C++ build script uses dependencies (include boost
) shipped as subprojects. Nothing needs to be provided externally.external-boost
In this mode Jinja2C++ build script uses only boost
as an externally-provided dependency. All other dependencies are taken from subprojects.external
In this mode all dependencies should be provided externally. Paths to boost
, nonstd-*
libs, etc. should be specified via standard CMake variables (like CMAKE_PREFIX_PATH
or libname_DIR)conan-build
Special mode for building Jinja2C++ via conan recipe.Jinja2C++ tries to use standard versions of std::variant
, std::string_view
and std::optional
if possible.
Thanks to @flexferrum for creating this library, for being one of the brightest minds in software engineering community. Rest in peace, friend.
Thanks to @manu343726 for CMake scripts improvement, bug hunting, and fixing and conan.io packaging.
Thanks to @martinmoene for the perfectly implemented xxx-lite libraries.
Thanks to @vitaut for the amazing text formatting library.
Thanks to @martinus for the fast hash maps implementation.
batch
filter addedslice
filter addedformat
filter addedtojson
filter addedstriptags
filter addedcenter
filter addedxmlattr
filter addedraw
/endraw
tags added'a' * 5
will produce 'aaaaa'
)meta
/endmeta
tags) added-fPIC
flag added to Linux build configurationblock
content if child doesn't override this blockvariant
, string_view
and optional
default
attribute added to the map
filter (#48)GenericList
type (#66)Value
filter
tag support added to the template parser (#44)escape
filter support added to the template parser (#140)capitalize
filter support added to the template parser (#137)set
tag added to the parser (#45)loop.depth
and loop.depth0
variables support addedTemplateEnv
*context
special param|
) operator precedence (#47)endblock
taginclude
and for
tagsJINJA2CPP_MSVC_RUNTIME_TYPE
CMake variableinclude
, import
and from
statements implemented. Now it's possible to include other templates and use macros from other templates.with
statement implementeddo
statement implementedRenderAsString
method now returns nonstd::expected
instead of regular std::string
import
, extends
and include
generate errors if parsed without TemplateEnv
setexternal
dependency management mode by defaultapplymacro
filter added which allows applying arbitrary macro as a filter