[
](https://github.com/pantor/inja/releases) Inja is a template engine for modern C++, loosely inspired by [jinja](http://jinja.pocoo.org) for python. It has an easy and yet powerful template syntax with all variables, loops, conditions, includes, callbacks, and comments you need, nested and combined as you like. Inja uses the wonderful [json](https://github.com/nlohmann/json) library by nlohmann for data input. Most importantly, inja needs only two header files, which is (nearly) as trivial as integration in C++ can get. Of course, everything is tested on all relevant compilers. Here is what it looks like: ```.cpp json data; data["name"] = "world"; inja::render("Hello {{ name }}!", data); // Returns "Hello world!" ``` ## Integration Inja is a headers only library, which can be downloaded from the [releases](https://github.com/pantor/inja/releases) or directly from the `include/` or `single_include/` folder. Inja uses `nlohmann/json.hpp` (>= v3.8.0) as its single dependency, so make sure it can be included from `inja.hpp`. json can be downloaded [here](https://github.com/nlohmann/json/releases). Then integration is as easy as: ```.cpp #includeWelcome to my blog!
{% endblock %} ``` calls a parent template with the `extends` keyword; it should be the first element in the template. It is possible to render the contents of the parent block by calling `super()`. In the case of multiple levels of `{% extends %}`, super references may be called with an argument (e.g. `super(2)`) to skip levels in the inheritance tree. ### Whitespace Control In the default configuration, no whitespace is removed while rendering the file. To support a more readable template style, you can configure the environment to control whitespaces before and after a statement automatically. While enabling `set_trim_blocks` removes the first newline after a statement, `set_lstrip_blocks` strips tabs and spaces from the beginning of a line to the start of a block. ```.cpp Environment env; env.set_trim_blocks(true); env.set_lstrip_blocks(true); ``` With both `trim_blocks` and `lstrip_blocks` enabled, you can put statements on their own lines. Furthermore, you can also strip whitespaces for both statements and expressions by hand. If you add a minus sign (`-`) to the start or end, the whitespaces before or after that block will be removed: ```.cpp render("Hello {{- name -}} !", data); // "Hello Inja!" render("{% if neighbour in guests -%} I was there{% endif -%} !", data); // Renders without any whitespaces ``` Stripping behind a statement or expression also removes any newlines. ### Comments Comments can be written with the `{# ... #}` syntax. ```.cpp render("Hello{# Todo #}!", data); // "Hello!" ``` ### Exceptions Inja uses exceptions to handle ill-formed template input. However, exceptions can be switched off with either using the compiler flag `-fno-exceptions` or by defining the symbol `INJA_NOEXCEPTION`. In this case, exceptions are replaced by `abort()` calls. ## Supported compilers Inja uses the `string_view` feature of the C++17 STL. Currently, the following compilers are tested: - GCC 7 - 11 (and possibly later) - Clang 5 - 12 (and possibly later) - Microsoft Visual C++ 2017 15.0 - 2022 (and possibly later) A list of supported compiler / os versions can be found in the [CI definition](https://github.com/pantor/inja/blob/master/.github/workflows/ci.yml).