diff options
author | Guillaume Gomez <guillaume1.gomez@gmail.com> | 2023-08-09 18:32:35 +0200 |
---|---|---|
committer | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2023-08-09 18:47:33 +0200 |
commit | 539debd690fe98e160c486290025a9a9a185543c (patch) | |
tree | 9bc4f27ccb01e135bdf8ef535652fb9885bae027 /book/src/template_syntax.md | |
parent | d4fbad1db221e780586d1849720e5bb6d70e982c (diff) | |
download | askama-539debd690fe98e160c486290025a9a9a185543c.tar.gz askama-539debd690fe98e160c486290025a9a9a185543c.tar.bz2 askama-539debd690fe98e160c486290025a9a9a185543c.zip |
Add section in book about using constants in templates
Diffstat (limited to 'book/src/template_syntax.md')
-rw-r--r-- | book/src/template_syntax.md | 22 |
1 files changed, 22 insertions, 0 deletions
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 +<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 |