commit | 27572db31aee81c325139c6c756ba6bcae5d8d09 | [log] [tgz] |
---|---|---|
author | pantor <lars.berscheid@online.de> | Wed Nov 22 08:11:18 2017 +0100 |
committer | pantor <lars.berscheid@online.de> | Wed Nov 22 08:11:18 2017 +0100 |
tree | 55a2e4a18f37281381ca7d32edfb171452c793d6 | |
parent | bb23f6e6a51bdb0f555c6ec94703cffdfa9a7531 [diff] |
fix parse bug
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, blocks, comments you need, nested and combined as you like. Inja uses the wonderful json library by nlohmann for data input and handling. 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. Have a look what it looks like:
json data; data["name"] = "world"; inja::render("Hello {{ name }}!", data); // "Hello World!"
Inja is a headers only library, which can be downloaded in the releases or directly from the src/
folder. Inja uses json by nlohmann as its single dependency, so make sure that it is included before inja. json can be found here.
#include "json.hpp" #include "inja.hpp" // For convenience using namespace inja; using json = nlohmann::json;
You can also integrate Inja in your project using Hunter, a package manager for C++.
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 exectuable code. Beside this tutorial, you can check the documentation for further information.
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
.
json data; data["name"] = "world"; render("Hello {{ name }}!", data); // "Hello World!" // For more advanced usage, an environment is recommended Environment env = Environment(); // Render a string with json data std::string result = env.render("Hello {{ name }}!", data); // Or directly read a template file Template temp = env.parse_template("./template.txt"); std::string result = temp.render(data); // "Hello World!" data["name"] = "Inja"; std::string result = temp.render(data); // "Hello Inja!" // Or read a json file for data directly from the environment result = env.render_template("./template.txt", "./data.json"); // Or write a rendered template file temp.write(data, "./result.txt") env.write("./template.txt", "./data.json", "./result.txt")
The environment class can be configured to your needs.
// With default settings Environment env_default = Environment(); // With global path to template files Environment env = Environment("../path/templates/"); // With global path where to save rendered files Environment env = Environment("../path/templates/", "../path/results/"); // Choose between JSON pointer or dot notation to access elements env.setElementNotation(inja::ElementNotation::Pointer); // (default) e.g. time/start env.setElementNotation(inja::ElementNotation::Dot); // e.g. time.start // With other opening and closing strings (here the defaults, as regex) env.setVariables("\\{\\{", "\\}\\}"); // Variables {{ }} env.setComments("\\{#", "#\\}"); // Comments {# #} env.setStatements("\\{\\%", "\\%\\}"); // Statements {% %} for many things, see below env.setLineStatements("##"); // Line statement ## (just an opener)
Variables are rendered within the {{ ... }}
expressions.
json data; data["neighbour"] = "Peter"; data["guests"] = {"Jeff", "Patrick", "Tom"}; data["time"]["start"] = 16; data["time"]["end"] = 22; // Indexing in array render("{{ guests/1 }}", data); // "Patrick" // Objects render("{{ time/start }} to {{ time/end }}pm"); // "16 to 22pm"
In general, the variables can be fetched using the JSON Pointer syntax. For convenience, the leading /
can be ommited. If no variable is found, valid JSON is printed directly, otherwise an error is thrown.
Statements can be written either with the {% ... %}
syntax or the ##
syntax for entire lines. The most important statements are loops, conditions and file includes. All statements can be nested.
// Combining loops and line statements render(R"(Guest List: ## for guest in guests {{ index1 }}: {{ guest }} ## endfor )", data) /* Guest List: 1: Jeff 2: Pierre 3: Tom */
In a loop, the special variables index (number)
, index1 (number)
, is_first (boolean)
and is_last (boolean)
are available.
Conditions support the typical if, else if and else statements. Following conditions are for example possible:
// Standard comparisons with variable render("{% if time/hour >= 18 %}…{% endif %}", data); // True // Variable in list render("{% if neighbour in guests %}…{% endif %}", data); // True // Logical operations render("{% if guest_count < 5 and all_tired %}…{% endif %}", data); // True // Negations render("{% if not guest_count %}…{% endif %}", data); // True
This include other files, relative from the current file location.
{% include "footer.html" %}
A few functions are implemented within the inja template syntax. They can be called with
// Upper and lower function, for string cases render("Hello {{ upper(neighbour) }}!", data); // "Hello PETER!" render("Hello {{ lower(neighbour) }}!", data); // "Hello peter!" // Range function, useful for loops render("{% for i in range(4) %}{{ index1 }}{% endfor %}", data); // "1234" // Length function (but please don't combine with range, use list directly...) render("I count {{ length(guests) }} guests.", data); // "I count 3 guests." // Round numbers to a given precision render({{ round(3.1415, 0) }}, data) // 3 render({{ round(3.1415, 3) }}, data) // 3.142 // Check if a value is odd, even or divisible by a number render({{ odd(42) }}, data) // false render({{ even(42) }}, data) // true render({{ divisibleBy(42, 7) }}, data) // true
Comments can be written with the {# ... #}
syntax.
render("Hello{# Todo #}!", data); // "Hello!"
Currently, the following compilers are tested:
Inja is licensed under the MIT License.