How to put braces in django templates?

前端 未结 3 916
终归单人心
终归单人心 2020-12-10 01:20

I need to produce an id surrounded by braces ( for example \"{1234}\" ). With the django template language, braces are also used to start a variable substitution, so I have

相关标签:
3条回答
  • 2020-12-10 01:55

    {% templatetag openbrace %} become extremely verbose for e.g. javascript templates

    I've used the verbatim tag from this gist with some success for exactly this purpose which lets you do something like

    {{ request.user }}
    {% verbatim %}
         brackets inside here are left alone, which is handy for e.g. jquery templates
         {{ this will be left }}
         {% so will this %}
    {% endverbatim }}
    
    {% more regular tags (to be replaced by the django template engine %}
    
    0 讨论(0)
  • 2020-12-10 01:57

    The recommendation from the Jinja templating language works with the Django templating engine as well:

    http://jinja.pocoo.org/docs/dev/templates/#escaping

    The solution is this:

    {{ '{' }}{{ id }}{{ '}' }}
    

    Of course the other two answers work, but this is one is less verbose and more readable, in my opinion.

    0 讨论(0)
  • 2020-12-10 02:03

    I think your answer can be found here:

    http://docs.djangoproject.com/en/dev/ref/templates/builtins/#templatetag

    In short, you want to use {% templatetag openbrace %} and {% templatetag closebrace %}.

    Edit: Django now also includes this functionality out of the box:

    {% verbatim %} {{ blah blah }} {% endverbatim %}
    
    0 讨论(0)
提交回复
热议问题