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

Implemented display_some and display_some_or #1014

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 10 additions & 0 deletions askama/src/filters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,11 @@ where
}
}

/// See [`display_some` in the Askama book] for more information.
///
/// See also [`display_some_or`].
///
/// [`display_some` in the Askama book]: https://djc.github.io/askama/filters.html#display_some
pub fn display_some<T>(value: &Option<T>) -> Result<DisplaySome<'_, T>>
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please put the filter functions before the types that need it.

Can we make DisplaySome and DisplaySomeOr private and yield impl Display from the filter functions?

where
T: fmt::Display,
Expand All @@ -373,6 +378,11 @@ where
}
}

/// See [`display_some_or` in the Askama book] for more information.
///
/// See also [`display_some`].
///
/// [`display_some_or` in the Askama book]: https://djc.github.io/askama/filters.html#display_some_or
pub fn display_some_or<'a, T, U>(
value: &'a Option<T>,
otherwise: U,
Expand Down
42 changes: 42 additions & 0 deletions book/src/filters.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ Enable it with Cargo features (see below for more information).
[`as_ref`][#as_ref],
[`capitalize`][#capitalize],
[`center`][#center],
[`display_some`][#display_some],
[`display_some_or`][#display_some_or],
[`escape|e`][#escape],
[`filesizeformat`][#filesizeformat],
[`fmt`][#fmt],
Expand Down Expand Up @@ -108,6 +110,46 @@ Output:
- a -
```

### display_some

[#display_some]: #display_some

The `display_some` filter is essentially a shorthand for:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I think we don't need the "a" before "shorthand".


```text
{% if let Some(value) = value %}{{ value }}{% endif %}
```

It can be used like this:

```text
<title>{{ title|display_some }}</title>
```

Where `title` can be any `Option<T>` as long as `T` implements [`fmt::Display`].

### display_some_or

[#display_some_or]: #display_some_or

The `display_some_or` filter is similar to `display_some`, but allows providing
a default value to render for `None`. In short, instead of the following:

```text
{% if let Some(value) = value %}{{ value }}{% else %}My default title{% endif %}
```

Then `display_some_or` can be used like this:

```text
<title>{{ title|display_some_or("My default title") }}</title>
```

Where `title` can be any `Option<T>` as long as `T` implements [`fmt::Display`].
While the `default` value can be any `U` implementing [`fmt::Display`].

[`fmt::Display`]: https://doc.rust-lang.org/std/fmt/trait.Display.html

### escape | e
[#escape]: #escape--e

Expand Down
14 changes: 14 additions & 0 deletions book/src/template_syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,20 @@ mirror Rust's [`if let` expressions]:
{% endif %}
```

See also the [`display_some`] and [`display_some_or`] filters, which
can be used to simplify _"render `Some` or nothing/default"_.

```text
<title>{{ title|display_some }}</title>

<title>{{ title|display_some_or("My default title") }}</title>
```

_Assuming `title` is `Option<String>`._

[`display_some`]: filters.html#display_some
[`display_some_or`]: filters.html#display_some_or

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

### Match
Expand Down
Loading