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

Implement is (not) defined feature #78

Merged
merged 6 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
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
37 changes: 37 additions & 0 deletions book/src/template_syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,43 @@ mirror Rust's [`if let` expressions]:

[`if let` expressions]: https://doc.rust-lang.org/reference/expressions/if-expr.html#if-let-expressions

### `is (not) defined`

You can use `is (not) defined` to ensure a variable exists (or not):

```jinja
{% if x is defined %}
x is defined!
{% endif %}
{% if y is not defined %}
y is not defined
{% else %}
y is defined
{% endif %}
```

You can combine conditions with this feature and even use it in expressions:

```jinja
{% if x is defined && x == "12" && y == Some(true) %}
...
{% endif %}

<script>
// It will generate `const x = true;` (or false is `x` is not defined).
const x = {{ x is defined }};
</script>
```

Due to proc-macro limitations, rinja can only see the fields of your current type and the variables
declared in the templates. Because of this, you can not check if a field or a function is defined:

```jinja
{% if x.y is defined %}
This code will not compile
{% endif %}
```

### Match

In order to deal with Rust `enum`s in a type-safe way, templates support
Expand Down
Loading