Inja
3.4.0
A Template Engine for Modern C++
|
Inja is a template engine for modern C++, loosely inspired by jinja 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. Of course, everything is tested in CI on all relevant compilers. Here is what it looks like:
Inja is a headers only library, which can be downloaded from the 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. Then integration is as easy as:
If you are using the Meson Build System, then you can wrap this repository as a subproject.
If you are using Conan to manage your dependencies, have a look at this repository. Please file issues here if you experience problems with the packages.
You can also integrate inja in your project using Hunter, a package manager for C++.
If you are using vcpkg on your project for external dependencies, then you can use the inja package. Please see the vcpkg project for any issues regarding the packaging.
If you are using cget, you can install the latest development version with cget install pantor/inja
. A specific version can be installed with cget install pantor/inja@v2.1.0
.
On macOS, you can install inja via Homebrew and brew install inja
.
If you are using conda, you can install the latest version from conda-forge with conda install -c conda-forge inja
.
This tutorial will give you an idea how to use inja. It will explain the most important concepts and give practical advices using examples and executable code. Beside this tutorial, you may check out the documentation.
The basic template rendering takes a template as a std::string
and a json
object for all data. It returns the rendered template as an std::string
.
For more advanced usage, an environment is recommended.
The environment class can be configured to your needs.
Variables are rendered within the {{ ... }}
expressions.
If no variable is found, valid JSON is printed directly, otherwise an inja::RenderError
is thrown.
Statements can be written either with the {% ... %}
syntax or the ##
syntax for entire lines. Note that ##
needs to start the line without indentation. The most important statements are loops, conditions and file includes. All statements can be nested.
In a loop, the special variables loop.index (number)
, loop.index1 (number)
, loop.is_first (boolean)
and loop.is_last (boolean)
are defined. In nested loops, the parent loop variables are available e.g. via loop.parent.index
. You can also iterate over objects like {% for key, value in time %}
.
Conditions support the typical if, else if and else statements. Following conditions are for example possible:
You can either include other in-memory templates or from the file system.
If a corresponding template could not be found in the file system, the include callback is called:
Inja will throw an inja::RenderError
if an included file is not found and no callback is specified. To disable this error, you can call env.set_throw_at_missing_includes(false)
.
Variables can also be defined within the template using the set statment.
Assignments only set the value within the rendering context; they do not modify the json object passed into the render
call.
A few functions are implemented within the inja template syntax. They can be called with
You can create your own and more complex functions with callbacks. These are implemented with std::function
, so you can for example use C++ lambdas. Inja Arguments
are a vector of json pointers.
You can also add a void callback without return variable, e.g. for debugging:
Template inheritance allows you to build a base skeleton template that contains all the common elements and defines blocks that child templates can override. Lets show an example: The base template
contains three blocks
that child templates can fill in. The child template
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.
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.
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:
Stripping behind a statement or expression also removes any newlines.
Comments can be written with the {# ... #}
syntax.
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.
Inja uses the string_view
feature of the C++17 STL. Currently, the following compilers are tested:
A list of supported compiler / os versions can be found in the CI definition.