Ale*_*lex 6 javascript regex mustache backbone.js underscore.js
我正在使用骨干的下划线模板引擎和胡子格式化模式.
我已经成功地在项目的其他地方使用它,但现在我第一次使用来自胡子的循环列表模式来填充模板,这会引发一个我感到有点困惑的错误.chrome中的错误是:
"Uncaught SyntaxError: Unexpected token ILLEGAL"
Run Code Online (Sandbox Code Playgroud)
并指向回溯中的下划线模板函数,这是相当无用的但是在firebug中我得到一个更有用的错误,如下所示:

建议哈希符号'#'是问题,这是有道理的,因为我知道胡子工作正常,因为项目的许多其他部分使用它很好,这也是我第一次使用哈希sybol在我的模板中.它看起来像循环功能或下划线的插值/模板设置的问题.
这是我的模板的相关部分:
<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>
Run Code Online (Sandbox Code Playgroud)
这里是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....}
Run Code Online (Sandbox Code Playgroud)
我最初在这里关注这个例子以供参考:http: //mustache.github.com/#demo
现在我在哪里想到问题可能是:
underscore.js建议在渲染胡子模板之前使用它:
_.templateSettings = {
evaluate : /\{\[([\s\S]+?)\]\}/g,
interpolate : /\{\{([\s\S]+?)\}\}/g
};
Run Code Online (Sandbox Code Playgroud)
也:
interpolate : /\{\{(.+?)\}\}/g
Run Code Online (Sandbox Code Playgroud)
也只是插入声明,我已经尝试了两种.但是我的正则表达式知识真的很差,我觉得它可能无法容纳哈希?无论如何......我完全被难倒了.有人可以帮帮我吗?
甚至可以像这样循环吗?看下划线源我不确定:http: //documentcloud.github.com/underscore/docs/underscore.html#section-120
非常感谢
小智 23
今天遇到了这个问题.问题似乎是Underscore执行模板的顺序:转义,插值,然后评估.所以你需要明确地忽略{{#插值正则表达式中的任何匹配:
_.templateSettings = {
evaluate: /\{\{#([\s\S]+?)\}\}/g, // {{# console.log("blah") }}
interpolate: /\{\{[^#\{]([\s\S]+?)[^\}]\}\}/g, // {{ title }}
escape: /\{\{\{([\s\S]+?)\}\}\}/g, // {{{ title }}}
}
Run Code Online (Sandbox Code Playgroud)
它实际上与Mustache的工作方式不同:Underscore的模板中没有正确的块,因此不需要关闭块{{/}}.您只需要像使用标准的Underscore模板一样匹配您的语句.
我发帖是因为遇到这个问题的其他人的缘故.经过大量的谷歌搜索无济于事,我通过一个精细的齿梳来检查underscore.js源代码,基本上你必须使用下划线的模板语法,在你的JSON中写入丑陋的函数处理器或者将mustache.js包含在你的源代码中并调用:
Mustache.render(mytemplate,mymodel)
Run Code Online (Sandbox Code Playgroud)
并且预示着下划线
_.template(..) function
Run Code Online (Sandbox Code Playgroud)
烦人但无论如何,我希望能有所帮助
| 归档时间: |
|
| 查看次数: |
7900 次 |
| 最近记录: |