Home > Software design >  How to have Django *not* parse "{{variable}}" when using Jsrender?
How to have Django *not* parse "{{variable}}" when using Jsrender?

Time:03-22

I am building a Django template which will use jsrender in order to build out dynamic Javascript templates for html code.

However, I find that when I put a Jsrender template in my code, Django flips out because Django parses anything between {{ and }} as a variable, and Jsrender uses this for its own variables. For instance, a template in Jsrender might look like:

    <script id="headerTemplate" type="text/x-jsrender">
        <h1>{{:label}}</h1>
    </script>

Django sees {{:label}} and tries to parse it, which leads to an error.

Is there a Django block I can invoke which would make Django not parse any text within it? Or otherwise to escape a { character?

CodePudding user response:

You can turn on the {% verbatim %} … {% endverbatim %} template tag [Django-doc]:

{% verbatim %}
    <script id="headerTemplate" type="text/x-jsrender">
        <h1>{{:label}}</h1>
    </script>
{% endverbatim %}

This will thus render the double curly brackets, not interpret these.

CodePudding user response:

You can choose alternative delimiters in JsRender, so as to avoid the conflict with Django delimiters.

https://www.jsviews.com/#settings/delimiters

For example:

$.views.settings.delimiters("<%", "%>");

will change the tag syntax to <%...%>.

  • Related