From c00ae7f58c34962ed6dbec4eb8aaf747da59ed2f Mon Sep 17 00:00:00 2001 From: Preston Timmons Date: Thu, 21 Jan 2016 21:50:06 -0600 Subject: [PATCH] Fixed #26118 -- Added 'is' operator to if template tag. --- django/template/smartif.py | 1 + docs/ref/templates/builtins.txt | 13 ++++++++++++- docs/releases/1.10.txt | 2 ++ tests/template_tests/syntax_tests/test_if.py | 10 ++++++++++ 4 files changed, 25 insertions(+), 1 deletion(-) diff --git a/django/template/smartif.py b/django/template/smartif.py index a3c1f37973..1433a8499a 100644 --- a/django/template/smartif.py +++ b/django/template/smartif.py @@ -98,6 +98,7 @@ OPERATORS = { 'not': prefix(8, lambda context, x: not x.eval(context)), 'in': infix(9, lambda context, x, y: x.eval(context) in y.eval(context)), 'not in': infix(9, lambda context, x, y: x.eval(context) not in y.eval(context)), + 'is': infix(10, lambda context, x, y: x.eval(context) is y.eval(context)), '==': infix(10, lambda context, x, y: x.eval(context) == y.eval(context)), '!=': infix(10, lambda context, x, y: x.eval(context) != y.eval(context)), '>': infix(10, lambda context, x, y: x.eval(context) > y.eval(context)), diff --git a/docs/ref/templates/builtins.txt b/docs/ref/templates/builtins.txt index bbc3ef063c..035a619a14 100644 --- a/docs/ref/templates/builtins.txt +++ b/docs/ref/templates/builtins.txt @@ -432,7 +432,7 @@ Use of actual parentheses in the :ttag:`if` tag is invalid syntax. If you need them to indicate precedence, you should use nested :ttag:`if` tags. :ttag:`if` tags may also use the operators ``==``, ``!=``, ``<``, ``>``, -``<=``, ``>=`` and ``in`` which work as follows: +``<=``, ``>=``, ``in``, and ``is`` which work as follows: ``==`` operator ^^^^^^^^^^^^^^^ @@ -524,6 +524,17 @@ you should use:: {% if a > b and b > c %} +``is`` operator +^^^^^^^^^^^^^^^ + +.. versionadded:: 1.10 + +Object identity. Tests if two values are the same object. Example:: + + {% if value is None %} + This will output if and only if value is None. + {% endif %} + Filters ~~~~~~~ diff --git a/docs/releases/1.10.txt b/docs/releases/1.10.txt index d0a7ba3b41..30e0660a51 100644 --- a/docs/releases/1.10.txt +++ b/docs/releases/1.10.txt @@ -291,6 +291,8 @@ Templates :class:`~django.template.backends.django.DjangoTemplates` backend and the :class:`~django.template.Engine` class. +* Added the ``is`` comparison operator to the :ttag:`if` tag. + Tests ~~~~~ diff --git a/tests/template_tests/syntax_tests/test_if.py b/tests/template_tests/syntax_tests/test_if.py index 92b3f16351..507a26b0b7 100644 --- a/tests/template_tests/syntax_tests/test_if.py +++ b/tests/template_tests/syntax_tests/test_if.py @@ -527,3 +527,13 @@ class IfTagTests(SimpleTestCase): # A single equals sign is a syntax error. with self.assertRaises(TemplateSyntaxError): self.engine.render_to_string('if-tag-single-eq', {'foo': 1}) + + @setup({'template': '{% if foo is True %}yes{% else %}no{% endif %}'}) + def test_if_is_match(self): + output = self.engine.render_to_string('template', {'foo': True}) + self.assertEqual(output, 'yes') + + @setup({'template': '{% if foo is True %}yes{% else %}no{% endif %}'}) + def test_if_is_no_match(self): + output = self.engine.render_to_string('template', {'foo': 1}) + self.assertEqual(output, 'no')