commit | d277cb0acda105a82278c0e926580e732754d23c | [log] [tgz] |
---|---|---|
author | pantor <lars.berscheid@online.de> | Tue Aug 15 15:52:52 2017 +0200 |
committer | pantor <lars.berscheid@online.de> | Tue Aug 15 15:52:52 2017 +0200 |
tree | 4ffd0e15abf7dd6a8676b14ae4509d57dfb87604 | |
parent | e03bb14b174d35f1b3a33846cc765aa26b37a8c9 [diff] |
code cleaning, add condition operators
json data; data["name"] = "world"; inja::render("Hello {{ name }}!", data); // "Hello World!"
Inja is headers only. Just one dependency: json by nlohmann.
#include "json.hpp" #include "inja.hpp" // For convenience using namespace inja; using json = nlohmann::json;
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 std::string result_template = env.render_temlate("template.txt", data); // And read a json file for data std::string result_template_2 = env.render_temlate_with_json_file("template.txt", "data.json");
The environment class can be configured.
// With default settings Environment env_default = Environment(); // With global path to template files Environment env_default = Environment("../path/templates/");
Variables can be rendered with the {{ ... }}
syntax.
json data; data["name"] = "world"; data["guests"] = {"Jeff", "Pierre", "Tom"}; data["time"]["start"]["hour"] = 16; data["time"]["end"]["hour"] = 21; string template = """ {{ guests/0 }} {{ time/start/hour }} to {{ time/end/hour }} or {{ 24 }} """;
Valid Json -> Printed. Json Pointer.
Statements can be written with the (% ... %)
syntax. The most important statements are loops, conditions and file includes.All statements can be nested.
In the loop, some special variables are available:
int index, index1
bool is_first
bool is_last
If, else if, else. Nested. conditions:
not
==
, >
, <
, >=
, <=
, !=
in
Include other files like (% include "footer.html" %)
. Relative from file.
Comments can be rendered with the {# ... #}
syntax.
render("Hello{# Todo #}!", data); // "Hello!"
Currently, the following compilers are tested:
The class is licensed under the MIT License.