aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLibravatar Guillaume Gomez <guillaume1.gomez@gmail.com>2023-08-09 18:32:35 +0200
committerLibravatar Dirkjan Ochtman <dirkjan@ochtman.nl>2023-08-09 18:47:33 +0200
commit539debd690fe98e160c486290025a9a9a185543c (patch)
tree9bc4f27ccb01e135bdf8ef535652fb9885bae027
parentd4fbad1db221e780586d1849720e5bb6d70e982c (diff)
downloadaskama-539debd690fe98e160c486290025a9a9a185543c.tar.gz
askama-539debd690fe98e160c486290025a9a9a185543c.tar.bz2
askama-539debd690fe98e160c486290025a9a9a185543c.zip
Add section in book about using constants in templates
-rw-r--r--book/src/template_syntax.md22
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