From 539debd690fe98e160c486290025a9a9a185543c Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Wed, 9 Aug 2023 18:32:35 +0200 Subject: Add section in book about using constants in templates --- book/src/template_syntax.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'book') diff --git a/book/src/template_syntax.md b/book/src/template_syntax.md index ec35b94..4483562 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 -- cgit