6

I am getting a TemplateSyntaxError: unexpected char u'#' error, when I include a simple Mustache template in my HTML file being served by Python Google App Engine server.

The mustache template that I want to include is:

{{#item}} {{name}} {{/item}}

My HTML file looks like this:

<!DOCTYPE html>
<html>
  <head>
    <script type="text/mustache-template" id="myTemplate">
      {{#item}}{{name}}{{/item}}
    </script>
  </head>
</html>

Since, the template is wrapped around a script tag with type=text/mustache-template, shouldn't the server just ignore it?

I am unable to comprehend, why am I getting the TemplateSyntaxError and what should I do to get rid of it. Anyone has any ideas? Thanks!

Nikhil Jindal
  • 1,053
  • 9
  • 10
  • 1
    You haven't said if the error is being generated by javascript or on the backend. Are you using a server based templating system as well, in which case stuff inside the script tags is usually processed as well, depending on the template system. – Tim Hoffman Jan 05 '14 at 11:27
  • The error is being generated on the backend. I am using Jinja2 templates on the server. – Nikhil Jindal Jan 06 '14 at 05:10

1 Answers1

8

You don't say so, but I guess you are using either Django or Jinja2 templates on the server side. In which case, no they wouldn't ignore content inside a mustache script tag: for one thing, they know nothing about mustache, and secondly it's fairly common practice to actually put server-side template tags inside Javascript, for instance to provide initial values for functions.

In Django versions greater than 1.5, you can wrap your mustache tags with the {% verbatim %}...{% endverbatim %} tag to prevent server-side evaluation. Jinja2's equivalent is {% raw %}...{% endraw %}.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895