I try to smuggle HTML template in the HTML for mustache.js, however the django template engine remove all the placeholders that should be output as-is to the front-end
The template is included in HTML in this way:
<script type="text/x-mustache-template" data-id="header_user_info"> <div id="header_user_info"> <div id="notification">0</div> <a href="#">{{username}}</a> </div> </script>
and I can get the HTML template by running $(el).html(), and generate html by using Mustache.to_html(temp, data);
I could put all the template into another static file and serve from CDN, but then it would be hard to track where the template belongs, and at least one extra http request.
Mustache is a logic-less template syntax. It can be used for HTML, config files, source code - anything. It works by expanding tags in a template using values provided in a hash or object. We call it "logic-less" because there are no if statements, else clauses, or for loops.
One way it does this is with the Template Language that is used to format and display content. Django comes with its own template language that is based on the popular Jinja2 template engine and adds some features that make it better suited for web development.
A mustache template is a string that contains any number of mustache tags. Tags are indicated by the double mustaches that surround them. {{person}} is a tag, as is {{#person}} . In both examples we refer to person as the tag's key.
Django Templates. Django provides a convenient way to generate dynamic HTML pages by using its template system. A template consists of static parts of the desired HTML output as well as some special syntax describing how dynamic content will be inserted.
You can simply change the tags:
Mustache.tags = ['[[', ']]'];
You can use the {% templatetag %}
templatetag to print out characters that would normally be processed by Django. For example:
{% templatetag openvariable %} variable {% templatetag closevariable %}
Results in the following in your HTML:
{{ variable }}
For a full list of arguments see: https://docs.djangoproject.com/en/dev/ref/templates/builtins/#templatetag
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With