mirror of
				https://github.com/django/django.git
				synced 2025-10-31 09:41:08 +00:00 
			
		
		
		
	Fixed #29060 -- Made {% firstof %} assign '' to the asvar if all inputs are false.
This commit is contained in:
		
				
					committed by
					
						 Tim Graham
						Tim Graham
					
				
			
			
				
	
			
			
			
						parent
						
							8dc675d90f
						
					
				
				
					commit
					d0b44c44ef
				
			| @@ -118,15 +118,16 @@ class FirstOfNode(Node): | |||||||
|         self.asvar = asvar |         self.asvar = asvar | ||||||
|  |  | ||||||
|     def render(self, context): |     def render(self, context): | ||||||
|  |         first = '' | ||||||
|         for var in self.vars: |         for var in self.vars: | ||||||
|             value = var.resolve(context, ignore_failures=True) |             value = var.resolve(context, ignore_failures=True) | ||||||
|             if value: |             if value: | ||||||
|                 first = render_value_in_context(value, context) |                 first = render_value_in_context(value, context) | ||||||
|  |                 break | ||||||
|         if self.asvar: |         if self.asvar: | ||||||
|             context[self.asvar] = first |             context[self.asvar] = first | ||||||
|             return '' |             return '' | ||||||
|         return first |         return first | ||||||
|         return '' |  | ||||||
|  |  | ||||||
|  |  | ||||||
| class ForNode(Node): | class ForNode(Node): | ||||||
|   | |||||||
| @@ -82,3 +82,10 @@ class FirstOfTagTests(SimpleTestCase): | |||||||
|         output = self.engine.render_to_string('firstof15', ctx) |         output = self.engine.render_to_string('firstof15', ctx) | ||||||
|         self.assertEqual(ctx['myvar'], '2') |         self.assertEqual(ctx['myvar'], '2') | ||||||
|         self.assertEqual(output, '') |         self.assertEqual(output, '') | ||||||
|  |  | ||||||
|  |     @setup({'firstof16': '{% firstof a b c as myvar %}'}) | ||||||
|  |     def test_all_false_arguments_asvar(self): | ||||||
|  |         ctx = {'a': 0, 'b': 0, 'c': 0} | ||||||
|  |         output = self.engine.render_to_string('firstof16', ctx) | ||||||
|  |         self.assertEqual(ctx['myvar'], '') | ||||||
|  |         self.assertEqual(output, '') | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user