mirror of
https://github.com/pantor/inja.git
synced 2026-04-20 14:59:28 +00:00
update readme
This commit is contained in:
91
README.md
91
README.md
@@ -1,16 +1,19 @@
|
||||
# Inja
|
||||
|
||||
A Template Engine for Modern C++
|
||||
|
||||
[](https://travis-ci.org/pantor/inja)
|
||||
[](https://coveralls.io/r/pantor/inja)
|
||||
[](https://www.codacy.com/app/pantor/inja?utm_source=github.com&utm_medium=referral&utm_content=pantor/inja&utm_campaign=Badge_Grade)
|
||||
[](http://github.com/pantor/inja/issues)
|
||||
[](https://raw.githubusercontent.com/pantor/inja/master/LICENSE)
|
||||
|
||||
|
||||
## Design
|
||||
|
||||
```c++
|
||||
json data = {{"name", "world"}};
|
||||
string result = inja::render("Hello {{ world }}!", data);
|
||||
// "Hello World!"
|
||||
json data;
|
||||
data["name"] = "world";
|
||||
|
||||
inja::render("Hello {{ name }}!", data); // "Hello World!"
|
||||
```
|
||||
|
||||
## Integration
|
||||
@@ -23,9 +26,82 @@ Inja is headers only. Just one dependency: json by nlohmann.
|
||||
```
|
||||
|
||||
|
||||
## Examples
|
||||
## Tutorial
|
||||
|
||||
|
||||
### Template Rendering
|
||||
```c++
|
||||
json data;
|
||||
data["name"] = "world";
|
||||
|
||||
inja::render("Hello {{ name }}!", data); // "Hello World!"
|
||||
|
||||
// For more advanced usage, an environment is recommended
|
||||
inja::Environment env = inja::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.
|
||||
```c++
|
||||
// With default settings
|
||||
inja::Environment env_default = inja::Environment();
|
||||
|
||||
// With global path to template files
|
||||
inja::Environment env_default = inja::Environment("../path/templates/");
|
||||
```
|
||||
|
||||
### Variables
|
||||
|
||||
Variables can be rendered with the `{{ ... }}` syntax.
|
||||
|
||||
|
||||
### Statements
|
||||
|
||||
Statements can be written with the `(% ... %)` syntax. The most important statements are loops, conditions and file includes.All statements can be nested.
|
||||
|
||||
#### Loops
|
||||
|
||||
```c++
|
||||
json data;
|
||||
data["guests"] = { "Jeff", "Pierre", "Tom" };
|
||||
|
||||
render("""Guests:
|
||||
(% for guest in guests %)- {{ guest }}
|
||||
(% endfor %)""", data);
|
||||
/* Guests:
|
||||
- Jeff
|
||||
- Pierre
|
||||
- Tom
|
||||
*/
|
||||
```
|
||||
|
||||
In the loop, some special variables are available:
|
||||
- int index
|
||||
- bool is_first
|
||||
- bool is_last
|
||||
|
||||
#### Conditions
|
||||
|
||||
#### Includes
|
||||
|
||||
Include other files like `(% include "footer.html" %)`. Relative from file.
|
||||
|
||||
### Comments
|
||||
|
||||
Comments can be rendered with the `{# ... #}` syntax.
|
||||
|
||||
```c++
|
||||
inja::render("Hello{# Todo #}!", data); // "Hello!"
|
||||
```
|
||||
|
||||
## Supported compilers
|
||||
|
||||
Currently, the following compilers are tested:
|
||||
@@ -33,6 +109,7 @@ Currently, the following compilers are tested:
|
||||
- GCC 4.9 - 7.1 (and possibly later)
|
||||
- Clang 3.6 - 3.7 (and possibly later)
|
||||
|
||||
|
||||
## License
|
||||
|
||||
The class is licensed under the [MIT License](https://raw.githubusercontent.com/pantor/inja/master/LICENSE).
|
||||
The class is licensed under the [MIT License](https://raw.githubusercontent.com/pantor/inja/master/LICENSE).
|
||||
|
||||
12
src/inja.hpp
12
src/inja.hpp
@@ -204,6 +204,7 @@ public:
|
||||
const std::regex regex_condition_close("endif");
|
||||
|
||||
std::smatch inner_statement_match;
|
||||
// Loop
|
||||
if (std::regex_match(statement_match.inner, inner_statement_match, regex_loop_open)) {
|
||||
SearchClosedMatch loop_match = search_close(input, regex_statement, regex_loop_open, regex_loop_close, statement_match);
|
||||
|
||||
@@ -211,11 +212,13 @@ public:
|
||||
string loop_command = inner_statement_match[0].str();
|
||||
result.push_back({{"type", "loop"}, {"command", loop_command}, {"inner", loop_match.inner}});
|
||||
}
|
||||
// Include
|
||||
else if (std::regex_match(statement_match.inner, inner_statement_match, regex_include)) {
|
||||
string include_command = inner_statement_match[0].str();
|
||||
string filename = inner_statement_match[1].str();
|
||||
result.push_back({{"type", "include"}, {"filename", filename}});
|
||||
}
|
||||
// Condition
|
||||
else if (std::regex_match(statement_match.inner, inner_statement_match, regex_condition_open)) {
|
||||
string if_command = inner_statement_match[0].str();
|
||||
json condition_result = {{"type", "condition"}, {"children", json::array()}};
|
||||
@@ -413,16 +416,17 @@ public:
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
string render(string input, json data) {
|
||||
return render(input, data, "./");
|
||||
}
|
||||
|
||||
string render(string input, json data, string path) {
|
||||
json parsed = parse(input);
|
||||
return render_tree(parsed, data, path);
|
||||
}
|
||||
|
||||
|
||||
string render(string input, json data) {
|
||||
return render(input, data, "./");
|
||||
}
|
||||
|
||||
string render_template(string filename, json data) {
|
||||
string text = load_file(filename);
|
||||
string path = filename.substr(0, filename.find_last_of("/\\") + 1); // Include / itself
|
||||
|
||||
Reference in New Issue
Block a user