本文介绍了获取TemplateSyntaxError:在python Google App Engine提供的HTML文件中包含Mustache模板时发生意外的字符'#'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在由Python Google App Engine服务器提供的HTML文件中包含一个简单的Mustache模板时,出现 TemplateSyntaxError:unexpected char u'#'错误。



我想包含的小胡子模板是:

lockquote

{{项目}}
{{名称}}
{{项目}}


这:

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

由于模板环绕着一个脚本标签,其中包含type = text / mustache-template,shouldn'服务器只是忽略它?



我无法理解,为什么我得到TemplateSyntaxError,我该怎么做才能摆脱它。
任何人有任何想法?
Thanks!

解决方案

你不这么说,但我猜你正在使用Django或Jinja2模板服务器端。在这种情况下,不会忽略胡须脚本标签中的内容:一方面,他们对胡须一无所知,其次,实际上将服务器端模板标签放入Javascript中是相当常见的做法,例如提供初始值在大于1.5的Django版本中,您可以使用 {%verbatim%} ... {%endverbatim %} 标记以防止服务器端评估。 Jinja2的等价物是 {%raw%} ... {%endraw%}


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:

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!

解决方案

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 %}.

这篇关于获取TemplateSyntaxError:在python Google App Engine提供的HTML文件中包含Mustache模板时发生意外的字符'#'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 05:45