-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #311 from GuillaumeGomez/runtime-values
Add support for "runtime" values
- Loading branch information
Showing
15 changed files
with
671 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# Runtime values | ||
|
||
It is possible to define variables at runtime and to use them in the templates using the `values` | ||
filter or the `rinja::get_value` function and to call the `_with_values` variants of the `render` | ||
methods. It expects an extra argument implementing the `Values` trait. This trait is implemented on | ||
a few types provided by the `std`, like `HashMap`: | ||
|
||
```rust | ||
use std::collections::HashMap; | ||
|
||
let mut values: HashMap<&str, Box<dyn Any>> = HashMap::new(); | ||
// We add a new value named "name" with the value "Bibop". | ||
values.insert("name", Box::new("Bibop")); | ||
values.insert("age", Box::new(12u32)); | ||
``` | ||
|
||
The `Values` trait is expecting types storing data with the `Any` trait, allowing to store any type. | ||
|
||
Then to render with these values: | ||
|
||
```rust | ||
template_struct.render_with_values(&values).unwrap(); | ||
``` | ||
|
||
There are two ways to get the values from the template, either by using the `value` filter | ||
or by calling directly the `rinja::get_value` function: | ||
|
||
```jinja | ||
{% if let Ok(name) = "name"|value::<&str> %} | ||
name is {{ name }} | ||
{% endif %} | ||
{% if let Ok(age) = rinja::get_value::<u32>("age") %} | ||
age is {{ age }} | ||
{% endif %} | ||
``` | ||
|
||
If you try to retrieve a value with the wrong type or that you didn't set, you will get an | ||
`Err(ValueError)`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.