Skip to content

Commit

Permalink
Add section in book about using constants in templates
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez authored and djc committed Aug 9, 2023
1 parent d4fbad1 commit 539debd
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions book/src/template_syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,28 @@ context,
while `{{ user.name }}` will get the ``name`` field of the ``user``
field from the template context.

## Using constants in templates

You can use constants defined in your Rust code. For example if you
have:

```rust
pub const MAX_NB_USERS: usize = 2;
```

defined in your crate root, you can then use it in your templates by
using ``crate::MAX_NB_USERS``:

```jinja
<p>The user limit is {{ crate::MAX_NB_USERS }}.</p>
{% set value = 4 %}
{% if value > crate::MAX_NB_USERS %}
<p>{{ value }} is bigger than MAX_NB_USERS.</p>
{% else %}
<p>{{ value }} is less than MAX_NB_USERS.</p>
{% endif %}
```

## Assignments

Inside code blocks, you can also declare variables or assign values
Expand Down

0 comments on commit 539debd

Please sign in to comment.