mirror of
https://github.com/pantor/inja.git
synced 2026-07-16 05:44:55 +00:00
readme update
This commit is contained in:
@@ -186,15 +186,15 @@ render({{ divisibleBy(42, 7) }}, data); // true
|
||||
|
||||
### Callbacks
|
||||
|
||||
You can create your own functions with callbacks. They can be added to the environment like
|
||||
You can create your own and more complex functions with callbacks.
|
||||
```c++
|
||||
Environment env = Environment();
|
||||
|
||||
/*
|
||||
* Callbacks are defined by its:
|
||||
* - name, which is equal to its function name
|
||||
* - name, which is equal to the function name
|
||||
* - number of arguments
|
||||
* - callback function. Using std::function, you can e.g. also use lambdas.
|
||||
* - callback function. Implemented with std::function, you can for example use lambdas.
|
||||
*/
|
||||
env.add_callback("double", 1, [&env](Parsed::Arguments args, json data) {
|
||||
const int number = env.renderer.eval_expression<int>(args[0], data); // Adapt the type and index of the argument
|
||||
@@ -204,7 +204,7 @@ env.add_callback("double", 1, [&env](Parsed::Arguments args, json data) {
|
||||
// You can then use a callback like a regular function
|
||||
env.render("{{ double(16) }}", data) // "32"
|
||||
|
||||
// A callback without argument can be used like a variable:
|
||||
// A callback without argument can be used like a dynamic variable:
|
||||
std::string greet = "Hello";
|
||||
env.add_callback("double-greetings", 0, [&env, greet](Parsed::Arguments args, json data) {
|
||||
return greet + " " + greet + "!";
|
||||
|
||||
Reference in New Issue
Block a user