百科.dev
全部条目AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
I

inja

> 编程语言
开源

一个适用于现代 C++ 的模板引擎

2.0K stars0 点赞0 次浏览
访问官网GitHub

工具介绍

一个适用于现代 C++ 的模板引擎

[ ](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. Of course, everything is tested in CI 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 #include // Just for convenience using namespace inja; ``` If you are using the [Meson Build System](http://mesonbuild.com), then you can wrap this repository as a subproject. If you are using [Conan](https://conan.io) to manage your dependencies, have a look at [this repository](https://github.com/DEGoodmanWilson/conan-inja). Please file issues [here](https://github.com/DEGoodmanWilson/conan-inja/issues) if you experience problems with the packages. You can also integrate inja in your project using [Hunter](https://github.com/cpp-pm/hunter), a package manager for C++. If you are using [vcpkg](https://github.com/Microsoft/vcpkg) on your project for external dependencies, then you can use the [inja package](https://github.com/Microsoft/vcpkg/tree/master/ports/inja). Please see the vcpkg project for any issues regarding the packaging. If you are using [cget](https://cget.readthedocs.io/en/latest/), you can install the latest development version with `cget install pantor/inja`. A specific version can be installed with `cget install pantor/[email protected]`. On macOS, you can install inja via [Homebrew](https://formulae.brew.sh/formula/inja#default) and `brew install inja`. If you are using [conda](https://docs.conda.io/en/latest/), you can install the latest version from [conda-forge](https://anaconda.org/conda-forge/inja) with `conda install -c conda-forge inja`. ## Tutorial 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](https://pantor.github.io/inja). ### Template Rendering 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`. ```.cpp json data; data["name"] = "world"; render("Hello {{ name }}!", data); // Returns std::string "Hello world!" render_to(std::cout, "Hello {{ name }}!", data); // Writes "Hello world!" to stream ``` For more advanced usage, an environment is recommended. ``` … ``` The environment class can be configured to your needs. ``` … ``` ### Variables Variables are rendered within the `{{ ... }}` expressions. ```.cpp json data; data["neighbour"] = "Peter"; data["guests"] = {"Jeff", "Tom", "Patrick"}; data["time"]["start"] = 16; data["time"]["end"] = 22; // Indexing in array render("{{ guests.1 }}", data); // "Tom" // Objects render("{{ time.start }} to {{ time.end + 1 }}pm", data); // "16 to 23pm" ``` If no variable is found, valid JSON is printed directly, otherwise an `inja::RenderError` is thrown. ### Statements 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. #### Loops ```.cpp // Combining loops and line statements render(R"(Guest List: ## for guest in guests {{ loop.index1 }}: {{ guest }} ## endfor )", data) /* Guest List: 1: Jeff 2: Tom 3: Patrick */ ``` 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 Conditions support the typical if, else if and else statements. Following conditions are for example possible: ```.cpp // Standard comparisons with a variable render("{% if time.hour >= 20 %}Serve{% else if time.hour >= 18 %}Make{% endif %} dinner.", data); // Serve dinner. // Variable in list render("{% if neighbour in guests %}Turn up the music!{% endif %}", data); // Turn up the music! // Logical operations render("{% if guest_count < (3+2) and all_tired %}Sleepy...{% else %}Keep going...{% endif %}", data); // Sleepy... // Negations render("{% if not guest_count %}The End{% endif %}", data); // The End ``` #### Includes You can either include other in-memory templates or from the file system. ```.cpp // To include in-memory templates, add them to the environment first inja::Template content_template = env.parse("Hello {{ neighbour }}!"); env.include_template("content", content_template); env.render("Content: {% include \"content\" %}", data); // "Content: Hello Peter!" // Other template files are included relative from the current file location render("{% include \"footer.html\" %}", data); ``` If a corresponding template could not be found in the file system, the *include callback* is called: ```.cpp // The callback takes the current path and the wanted include name and returns a template env.set_include_callback([&env](const std::filesystem::path& path, const std::string& template_name) { return env.parse("Hello {{ neighbour }} from " + template_name); }); // You can disable to search for templates in the file system via env.set_search_included_templates_in_files(false); ``` 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)`. #### Assignments Variables can also be defined within the template using the set statment. ```.cpp render("{% set new_hour=23 %}{{ new_hour }}pm", data); // "23pm" render("{% set time.start=18 %}{{ time.start }}pm", data); // using json pointers ``` Assignments only set the value within the rendering context; they do not modify the json object passed into the `render` call. ### Functions A few functions are implemented within the inja template syntax. They can be called with ``` … ``` The Jinja2 pipe call syntax of functions is also supported: ```.cpp // Upper neighbour value render("Hello {{ neighbour | upper }}!", data); // "Hello PETER!" // Sort array and join with comma render("{{ [\"B\", \"A\", \"C\"] | sort | join(\",\") }}", data); // "A,B,C" ``` ### Callbacks 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: ```.cpp env.add_void_callback("log", 1, [greet](Arguments args) { std::cout << "logging: " << args[0] << std::endl; }); env.render("{{ log(neighbour) }}", data); // Prints nothing to result, only to cout... ``` ### Template Inheritance 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 ```.html {% block head %} {% block title %}{% endblock %} - My Webpage {% endblock %} ``` contains three `blocks` that child templates can fill in. The child template ```.html {% extends "base.html" %} {% block title %}Index{% endblock %} {% block head %} {{ super() }} {% endblock %} {% block content %}

Index

Welcome 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. ### HTML escaping Templates are frequently used to creat HTML pages. Source data that contains characters that have meaning within HTML (like <. >, &) needs to be escaped. It is often inconvenient to perform such escaping within the JSON data. With `Environment::set_html_autoescape(true)`, Inja can be configured to HTML escape each and every string created. ### 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 8 - 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).

GitHub Issues· 0 开放

在 GitHub 查看全部

暂无开放 Issues,或尚未同步最近议题。

核心特点

  • •GCC 8 - 11 (and possibly later)
  • •Clang 5 - 12 (and possibly later)
  • •Microsoft Visual C++ 2017 15.0 - 2022 (and possibly later)

> 标签

C++header-onlyinjajinjajson

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言