-
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.
Add new "runtime values" chapter in the book
- Loading branch information
1 parent
3e4be06
commit 4206409
Showing
2 changed files
with
36 additions
and
0 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,35 @@ | ||
# Runtime values | ||
|
||
It is possible to define variables at runtime and to use them in the templates using the `VALUES` | ||
variable. To do so you need to use the `_with_values` variants of the `render` methods. It expects | ||
an extra argument of type `Values`: | ||
|
||
```rust | ||
use rinja::Values; | ||
|
||
let mut values = Values::new(); | ||
// We add a new value named "name" with the value "Bibop". | ||
values.add("name", "Bibop"); | ||
// We add a new value named "another" with a vec. | ||
values.add("another", vec![false]); | ||
``` | ||
|
||
The `Values` type is storing data with the `Any` trait, allowing to store any type as long as it | ||
implements this trait. | ||
|
||
Then to render with these values: | ||
|
||
```rust | ||
TemplateStruct.render_with_values(&values).unwrap(); | ||
``` | ||
|
||
And to use them in a template: | ||
|
||
```jinja | ||
{% if let Ok(name) = VALUES.get::<&str>() %} | ||
name is {{ name }} | ||
{% endif %} | ||
``` | ||
|
||
If you try to retrieve a value with the wrong type or that you didn't set, you will get an | ||
`Err(ValueError)`. |