jinja2cpp / Jinja2Cpp

Jinja2 C++ (and for C++) almost full-conformance template engine implementation
https://jinja2cpp.github.io
Mozilla Public License 2.0
482 stars 81 forks source link
c-plus-plus cpp cpp17 cpp20 jinja jinja2 jinja2-templates jinja2-templating-engine string-formatter string-template template-engine templates

Jinja2С++

Language Standard Standard Standard Coverage Status Github Releases Github Issues GitHub License conan.io Gitter Chat

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.

Introduction

Main features of Jinja2C++:

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!!!

Getting started

To use Jinja2C++ in your project you have to:

Usage of Jinja2C++ in the code is pretty simple:

  1. Declare the jinja2::Template object:
jinja2::Template tpl;
  1. Populate it with template:
tpl.Load("{{ 'Hello World' }}!!!");
  1. Render the template:
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

Current Jinja2 support

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.

Supported compilers

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.

Build status

Compiler Status
MSVC 2015 (x86, x64), MinGW 7 (x64), MinGW 8 (x64) Build status
X-Code 9, 10, 11 Build Status
MSVC 2017 (x86, x64), MSVC 2019 (x86, x64), C++14/C++17
g++ 5, 6, 7, 8, 9, 10, 11 clang 5, 6, 7, 8, 9, 10, 11, 12 C++14/C++17/C++20

Build and install

Jinja2C++ has several external dependencies:

Examples 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:

  1. Install CMake build system (at least version 3.0)
  2. Clone jinja2cpp repository:
> git clone https://github.com/flexferrum/Jinja2Cpp.git
  1. Create build directory:
> cd Jinja2Cpp
> mkdir build
  1. Run CMake and build the library:
> 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.

  1. Install library:
> 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.

Usage with conan.io dependency manager

Jinja2C++ can be used as conan.io package. In this case, you should do the following steps:

  1. Install conan.io according to the documentation ( https://docs.conan.io/en/latest/installation.html )
  2. Add a reference to Jinja2C++ package (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)

Additional CMake build flags

You can define (via -D command-line CMake option) the following build flags:

Build with C++17 standard enabled

Jinja2C++ tries to use standard versions of std::variant, std::string_view and std::optional if possible.

Acknowledgments

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.

Changelog

Version 1.3.2

What's Changed

New Contributors

Version 1.3.1

Changes and improvements

Fixed bugs

Breaking changes

Version 1.2.1

Changes and improvements

Fixed bugs

Breaking changes

Version 1.1.0

Changes and improvements

Fixed bugs

Breaking changes

Version 1.0.0

Changes and improvements

Fixed bugs

Breaking changes

Version 0.9.2

Major changes

Minor changes

Breaking changes

Version 0.9.1

Version 0.9

Version 0.6