Opened 3 years ago

Last modified 3 years ago

#23424 assigned Bug

Verbatim tag fails to render curly braces

Reported by: stephband Owned by: Paul Egges
Component: Template system Version: master
Severity: Normal Keywords: verbatim template tag
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Tim Graham)

The {% verbatim %} tag fails to render curly braces under certain conditions - notably when there are two verbatim tags on the same line.

This works:

{% verbatim %}{{{% endverbatim %}   – correctly renders as –   {{
{% verbatim %}}}{% endverbatim %}   – correctly renders as –   }}
{% verbatim %}{ {% endverbatim %}text{% verbatim %} }{% endverbatim %}   – correctly renders as – { text }

However, this fails:

{% verbatim %}{{% endverbatim %}text{% verbatim %}}{% endverbatim %}   – incorrectly renders as –   {{% endverbatim %}text{% verbatim %}}
{% verbatim %}{{{% endverbatim %}text{% verbatim %}}}{% endverbatim %}   – incorrectly renders as –   {{{% endverbatim %}text{% verbatim %}}}
{% verbatim %}{{ {% endverbatim %}text{% verbatim %} }}{% endverbatim %}   – incorrectly renders as –   {{ {% endverbatim %}text{% verbatim %} }}

Change History (10)

comment:1 Changed 3 years ago by stephband

Description: modified (diff)

comment:2 Changed 3 years ago by stephband

Description: modified (diff)

comment:3 Changed 3 years ago by Tim Graham

Description: modified (diff)

comment:4 Changed 3 years ago by Tim Graham

Triage Stage: UnreviewedAccepted
Version: 1.7-rc-2master

Not sure if it will be easy/possible to fix. If not, we can document the limitation.

comment:5 Changed 3 years ago by Paul Egges

This occurs because the {{ and }} are the starting and ending delimiters for variables. This also affects block tag delimters, {% and %}, and comment tag delimiters, {# and #}. In all cases they must be matching.

comment:6 Changed 3 years ago by Paul Egges

Owner: changed from nobody to Paul Egges
Status: newassigned

comment:7 Changed 3 years ago by shayneoneill

Possibly a better example why this is a really huge headache is this

{%verbatim%} {{ {%endverbatim%} computer.{{field}} {%verbatim%} }} {%endverbatim%}

From some auditing software where we are using django views to render lists of fields , which however are being fetched via AngularJS from a third party REST source. We simply can't find a simple way around this. :(

An alternative might even be some simple tags that provide the missing character Ie {% special left_double_brace %} or whatever.

Whilst documentation would at least warn the bug is there, this is a show-stopper error for a lot of use cases.

For the purposes of anyone coming across this ticket and pulling their hair out over it, heres a template tag that might ease the pain;-

from django import template

register = template.Library()

@register.filter(name='specialbracket')
def specialbracket(value):
    if value == 'left':
        return "{{"
    elif value == 'right':
        return "}}"
    else:
        return "??"

And the above example solved with this tag;-

        <td class="field-{{ident}}">{{ 'left'|specialbracket }} computer.{{field}} {{ 'right'|specialbracket }} </td>

Last edited 3 years ago by shayneoneill (previous) (diff)

comment:8 Changed 3 years ago by Aymeric Augustin

Shayne, aren't you looking for the templatetag tag?

comment:9 Changed 3 years ago by shayneoneill

Oh wow. I didn't even realise that existed. Thanks! Still I do think that allowing multiple verbatim/endverbatim instances on a line would solve a lot of headaches.

comment:10 Changed 3 years ago by Aymeric Augustin

This may require switching to a proper lexer that understands literals instead of the current regex-based implementation.

Note: See TracTickets for help on using tickets.
Back to Top