|
1 | 1 | from django.template import Library, Node, TemplateSyntaxError, Variable, VariableDoesNotExist |
2 | 2 | from django.template.base import render_value_in_context |
| 3 | +from django.utils.html import escape, format_html |
3 | 4 | from django.utils.safestring import SafeData, mark_safe |
4 | 5 |
|
5 | 6 | register = Library() |
@@ -27,20 +28,28 @@ def render(self, context): |
27 | 28 | value = render_value_in_context(output, context) |
28 | 29 | # Restore percent signs. Percent signs in template text are doubled |
29 | 30 | # so they are not interpreted as string format flags. |
30 | | - is_safe = isinstance(value, SafeData) |
31 | 31 | value = value.replace('%%', '%') |
32 | 32 |
|
33 | 33 | formatvalues = [] |
34 | 34 | for formatvalue in self.formatvalues: |
| 35 | + variable_value = None |
35 | 36 | try: |
| 37 | + # Try to see if this value is actually a variable reference |
36 | 38 | variable = Variable(formatvalue) |
37 | | - formatvalues.append(variable.resolve(context)) |
| 39 | + |
| 40 | + variable_value = variable.resolve(context) |
38 | 41 | except VariableDoesNotExist: |
39 | | - formatvalues.append(formatvalue) |
| 42 | + # If not, it's just a plain value specified directly |
| 43 | + variable_value = formatvalue |
| 44 | + |
| 45 | + # Check if the string isn't marked as safe, in which case, escape. |
| 46 | + if not isinstance(variable_value, SafeData): |
| 47 | + variable_value = escape(variable_value) |
| 48 | + |
| 49 | + formatvalues.append(variable_value) |
40 | 50 |
|
41 | 51 | value = value.format(*formatvalues) |
42 | 52 |
|
43 | | - value = mark_safe(value) if is_safe else value |
44 | 53 | if self.asvar: |
45 | 54 | context[self.asvar] = value |
46 | 55 | return '' |
|
0 commit comments