我正在使用骨干的下划线模板引擎和胡子格式化模式。
我已经在项目的其他地方成功地使用了它,但现在我第一次使用 Mustache 中的循环列表模式来填充模板,该模板引发了一个我有点困惑的错误。
chrome 中的错误是:
"Uncaught SyntaxError: Unexpected token ILLEGAL"
并指向回溯中下划线的模板函数,这几乎没有用,但在 firebug 中我得到了一个更有用的错误,如下所示:
建议哈希符号“#”是问题所在,这是有道理的,因为我知道 Mustache 工作正常,因为项目的许多其他部分都很好地使用了它,这也是这我第一次在模板中使用哈希符号。看起来循环功能或下划线的插值/模板设置有问题。
这是我的模板的相关部分:
<div class="thumblist thumblistleft" id="currentprojectslist">
<div class="thumb-list-header">
<h2>current projects</h2>
</div>
<div class="thumb-list-area">
<ol>
{{#worklist}} <!----- LOOK HERE --->
{{#current}}
<li><a>{{title}}</a></li>
{{/current}}
{{/worklist}}
</ol>
</div>
</div>
这是一个 JSON 示例(全部验证正常),
{blah blah blah lot in here before,"worklist":[{"thumb":"img/project-s.jpg","id":"340","title":"Test Project One","desc":"big load of content here","current":true}], and so on....}
我最初在这里遵循此示例以供参考:
http://mustache.github.com/#demo
现在我认为问题出在哪里可能是:
underscore.js 建议在渲染胡子模板之前使用它:
_.templateSettings = {
evaluate : /\{\[([\s\S]+?)\]\}/g,
interpolate : /\{\{([\s\S]+?)\}\}/g
};
另外:
interpolate : /\{\{(.+?)\}\}/g
也只是插值语句,我都尝试过。然而我的正则表达式知识真的很差,我有一种感觉它可能无法容纳哈希?无论如何……我完全被难住了。
有人可以帮我吗?
甚至可以这样循环吗?看看下划线来源我不确定:
http://documentcloud.github.com/underscore/docs/underscore.html #section-120
非常感谢
I'm using backbone's underscore templating engine with the mustache formatting patterns.
I have already been successfully using it elsewhere in the project but now for the first time I'm using the looping list patterns from mustache to populate the template which is throwing an error which I'm a bit baffled by.
The error in chrome is:
"Uncaught SyntaxError: Unexpected token ILLEGAL"
and points to underscore's template function in the backtrace, which is pretty useless but in firebug i get a more helpful error like this:
Suggesting that the hash symbol '#' is the issue, which would make sense as I know that mustache is working ok as there are many other parts of the project using it well, also this is the first time I'm using the hash sybol in my templates.It looks like a problem either with the looping feature or with the interpolation/template settings for underscore.
Here's the relevant piece of my template:
<div class="thumblist thumblistleft" id="currentprojectslist">
<div class="thumb-list-header">
<h2>current projects</h2>
</div>
<div class="thumb-list-area">
<ol>
{{#worklist}} <!----- LOOK HERE --->
{{#current}}
<li><a>{{title}}</a></li>
{{/current}}
{{/worklist}}
</ol>
</div>
</div>
and here's a sample of the JSON (which all validates fine)
{blah blah blah lot in here before,"worklist":[{"thumb":"img/project-s.jpg","id":"340","title":"Test Project One","desc":"big load of content here","current":true}], and so on....}
I was initially following this example here for reference:
http://mustache.github.com/#demo
NOW HERES WHERE I THINK THE PROBLEM MIGHT BE:
underscore.js suggests using this before rendering a mustache template:
_.templateSettings = {
evaluate : /\{\[([\s\S]+?)\]\}/g,
interpolate : /\{\{([\s\S]+?)\}\}/g
};
also:
interpolate : /\{\{(.+?)\}\}/g
Also just the interpolate statement, ive tried both. However my regex knowledge is really poor and I have a feeling it might not accomodate the hash? At any rate.... I'm totally stumped.
Can someone help me out here?
is it even possible to loop like this? looking at underscore source i'm not sure:
http://documentcloud.github.com/underscore/docs/underscore.html#section-120
Thanks very much
发布评论
评论(3)
今天遇到了这个问题。问题似乎在于 Underscore 进行模板化的顺序:转义、插值,然后求值。因此,您需要显式忽略插值正则表达式中
{{#
的任何匹配项:它实际上与 Mustache 的工作方式不同:Underscore 模板中没有正确的块,因此没有需要一个结束块
{{/}}
。您只需像使用标准下划线模板一样匹配您的语句即可。Came up against this problem today. The issue seems to be the order that Underscore does the templating: escape, interpolate, then evaluate. So you need to explicitly ignore any matches for
{{#
in your interpolation regex:It doesn't actually work the same way as Mustache: there aren't proper blocks in Underscore's templating, so there is no need for a closing block
{{/}}
. You just need to match your statements up like you would with standard Underscore templates.我发帖是为了其他面临这个问题的人。
经过大量谷歌搜索无果后,我仔细地浏览了 underscore.js 源代码,基本上你必须使用 underscore 的模板语法,将丑陋的函数处理器写入你的 JSON 或将 Mustache.js 包含到你的源代码中并调用:
并放弃下划线的
烦人但无论如何,我希望能帮助别人
I'm posting for the sakes of anyone else facing this issue.
After a lot of googling to no avail, i went through the underscore.js source with a fine toothed comb and basically you have to either use underscore's template syntax, writein ugly function processors into your JSON or include mustache.js into your source and call:
and foresake underscore's
Annoying but whatever, I hope that helps someone
我没有使用 # 符号,但当我尝试使用三重胡子
{{{name}}}
来获取未转义值的设置时,我遇到了类似的错误:如果这就是您来到这里的原因,以下设置有效
我尝试了Max的格式,但这对我不起作用,因为我混合了双胡子和三胡子表达式,虽然三胡子表达式工作正常,但它从双胡子中变量名的每一端剥离一个字符IE
{{title}}
导致下划线查找名为itl
的变量I am not using the # symbol but I experienced a similar error once I tried to use the triple mustache
{{{name}}}
for unescaped values with the setting:If that's the reason you came here, the following setting works
I tried Max's format but that didn't work for me because I have a mix of double and triple mustache expressions and while the triple expression worked fine it was stripping a character from each end of the variable name in the double mustache i.e.
{{title}}
led to underscore looking for a variable nameditl