Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django and Mustache use the same syntax for template

Tags:

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.

like image 670
DB Tsai Avatar asked Nov 02 '11 18:11

DB Tsai


People also ask

What is mustache syntax?

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.

What template system does Django use?

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.

What is in mustache template?

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.

What are templates in Django or Django template language?

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.


2 Answers

You can simply change the tags:

Mustache.tags = ['[[', ']]']; 
like image 64
surj Avatar answered Sep 30 '22 03:09

surj


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

like image 26
Chris Pratt Avatar answered Sep 30 '22 03:09

Chris Pratt