Add a as_type= argument for {% definevar %} for casting to types.

Review Request #14698 — Created Nov. 18, 2025 and updated

Information

Djblets
release-5.x

Reviewers

{% definevar %} can now store a value as a boolean or integer through
the as_type= argument. By default, this stored as a string
(equivalent to str).

When passing as_type=bool, a true (lowercased) or 1 will
result in a stored True. Anything else will be False.

When passing as_type=int, the value will be converted to an integer.
If the value can't be parsed as an integer, a TemplateSyntaxError will
be raised.

There's also an optimization around the options. We now convert to a
set so we don't repeatedly iterate through the list for each option.

Unit tests pass.

Made use of this in some in-progress code in Review Board for setting
options.

Summary ID
Add a as_type= argument for {% definevar %} for casting to types.
`{% definevar %}` can now store a value as a boolean or integer through the `as_type=` argument. By default, this stored as a string (equivalent to `str`). When passing `as_type=bool`, a `true` (lowercased) or `1` will result in a stored `True`. Anything else will be `False`. When passing `as_type=int`, the value will be converted to an integer. If the value can't be parsed as an integer, a `TemplateSyntaxError` will be raised. There's also an optimization around the options. We now convert to a `set` so we don't repeatedly iterate through the list for each option.
b07678ceb1eb5292b075300623226a4663108dc6
Description From Last Updated

The end sounds a bit weird, maybe say "... before use."

maubinmaubin

Shouldn't here and the "Version Added"s below all be "Version Changed" instead?

maubinmaubin

Shouldn't the result of this always be str, not Any? Given that this is essentially a parse step, it would …

daviddavid

Perhaps we should raise TemplateSyntaxError if 'spaceless', 'strip', or 'unsafe' are provided but we're casting to bool or int?

daviddavid

You're stripping twice here.

daviddavid

Can we add tests for "1", "TRUE", "0", "false", as well as a few including whitespace (" true", " 1 …

daviddavid

Can we add tests with whitespace, 0, and negative numbers?

daviddavid
Checks run (2 succeeded)
flake8 passed.
JSHint passed.
maubin
  1. 
      
  2. djblets/util/templatetags/djblets_utils.py (Diff revision 1)
     
     
     
    Show all issues

    The end sounds a bit weird, maybe say "... before use."

  3. djblets/util/templatetags/djblets_utils.py (Diff revision 1)
     
     
     
     
     
     
     
     
     
     
     
    Show all issues

    Shouldn't here and the "Version Added"s below all be "Version Changed" instead?

  4. 
      
david
  1. 
      
  2. Show all issues

    Shouldn't the result of this always be str, not Any?

    Given that this is essentially a parse step, it would be nice to assign the result after the parse to a separate variable which is explicitly typed as str | SafeString | int | bool instead of reassigning to result

  3. djblets/util/templatetags/djblets_utils.py (Diff revision 1)
     
     
     
     
     
    Show all issues

    Perhaps we should raise TemplateSyntaxError if 'spaceless', 'strip', or 'unsafe' are provided but we're casting to bool or int?

  4. djblets/util/templatetags/djblets_utils.py (Diff revision 1)
     
     
     
     
     
    Show all issues

    You're stripping twice here.

  5. Show all issues

    Can we add tests for "1", "TRUE", "0", "false", as well as a few including whitespace (" true", " 1 ")?

  6. Show all issues

    Can we add tests with whitespace, 0, and negative numbers?

  7.