From 6f62446b9fd63ba64cadc8fdae7638b10194c843 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Wed, 9 Aug 2023 18:32:35 +0200 Subject: [PATCH] Add section in book about using constants in templates --- book/src/template_syntax.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/book/src/template_syntax.md b/book/src/template_syntax.md index ec35b947f..4483562c4 100644 --- a/book/src/template_syntax.md +++ b/book/src/template_syntax.md @@ -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 +

The user limit is {{ crate::MAX_NB_USERS }}.

+{% set value = 4 %} +{% if value > crate::MAX_NB_USERS %} +

{{ value }} is bigger than MAX_NB_USERS.

+{% else %} +

{{ value }} is less than MAX_NB_USERS.

+{% endif %} +``` + ## Assignments Inside code blocks, you can also declare variables or assign values