How to escape {{ or }} in django template?

前端 未结 10 1898
慢半拍i
慢半拍i 2020-12-01 04:17

Django treats {{ var }} as some variable in its template. How can I escape {{ var }} or {{ or }} such that django does no

相关标签:
10条回答
  • 2020-12-01 04:38

    This template tag (designed for use with jQuery Templates) might do the trick. It let's you wrap content you don't want Django to interpret as variables with a template tag.

    0 讨论(0)
  • 2020-12-01 04:41

    if you simply need to use {{ }} as a variable for template framework like angularjs, then following maybe simpler:

    in your <app path>/templatetags/ngvar.py , add

    from django import template
    register = template.Library()
    
    @register.simple_tag
    def ngvar(var_name):
        return "{{%s}}" % var_name
    

    and in template, do

    {% load ngvar %}
    {% ngvar "variable name" %}
    

    if ngvar.py is the first template tag, then make sure to add __init__.py file to the templatetags directory

    0 讨论(0)
  • 2020-12-01 04:42

    Although the above answers can solve the original problem, I add some hack around here for those who are scratching their heads like me.

    Some times, we want to render a single brace followed by a variable. For example, in BibTeX, there may be something look like this:

    @MISC{hu2012-spectral,
        author = {Hu, Pili},
        title = {Spectral Clustering Survey},
        howpublished = {GitHub, https://github.com/hupili/tutorial/tree/master/spectral-clustering},
        month = {May},
        year = {2012}
    }
    

    Those bib fields come from template variables. If you write

    title = {{{title}}},
    

    jinja can not compile and raise an error. If you write

    title = { {{title}} },
    

    there will be extra blanks. The hack around is to store '{' and '}' as variables and use later.

    {% set lb = '{' %}
    {% set rb = '}' %}
    ...
    @MISC{{lb}}{{ meta.bib_key }},
        author = {{lb}}Hu, Pili{{rb}},
        title = {{lb}}{{ meta.title }}{{rb}},
        howpublished = {{lb}}GitHub, https://github.com/hupili/tutorial/tree/master/{{ auto.path}}{{rb}},
        month = {{lb}}{{ meta.month }}{{rb}},
        year = {{lb}}{{ meta.year }}{{rb}}
    }
    

    This looks clumsy but it is the best I find so far. If you have a cleaner solution, please tell me.

    0 讨论(0)
  • 2020-12-01 04:45

    You can try escaping with html character escapes like:

    { = &#123;

    } = &#125;

    <p>"&#123;&#123; some text &#125;&#125;"</p>
    

    Try that inside your browser.

    0 讨论(0)
  • 2020-12-01 04:45

    it can be solved by avoing adjacent angular backets, if its inside javascript code then you can write

    '{'+'{address.'+key+'}}'
    

    I used this to print jinja variables into another template,using javascript.

    0 讨论(0)
  • 2020-12-01 04:46

    Another option would be to add a word joiner (zero width no-break space) between each curly bracket:

    <p>"{&#8288;{ some text }&#8288;}"</p>
    
    0 讨论(0)
提交回复
热议问题