Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add section in book about using constants in templates #853

Merged
merged 1 commit into from
Aug 9, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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