3

尝试解析存储为多行字符串的下划线模板时出现错误:

{{ _.each(records, function(record, index) { }}\
    <tr>\
        {{ record.get("hours") }}\
    </tr>\
{{ }) }}\

错误:

Uncaught SyntaxError: Unexpected token )

在第 1 行(_.each 行)。

根据下划线的文档,语法对我来说是正确的。

编辑:我应该注意我在我的模板中使用 {{ 而不是 <%= 并且改回 <%= 并不能解决问题。

编辑:这是我用于评估的正则表达式:

// Underscore templates should use {{ variable_name }} instead of <%= variable_name =%>
_.templateSettings = {
    interpolate: /\{\{(.+?)\}\}/g
};
4

1 回答 1

4

您需要使用{[, 进行评估。

假设您首先定义了一个正则表达式来更改 underscore.js 用于评估的默认符号,例如

 _.templateSettings = {
       evaluate: /\{\[([\s\S]+?)\]\}/g,
       interpolate: /\{\{([\s\S]+?)\}\}/g, 
       escape: /\{\{-([\s\S]+?)\}\}/g
 };

然后你可以做类似的事情

{[ _.each(records, function(record, index) { ]}
    <tr>
        {{ record.get("hours") }}
    </tr>
{[ },this); ]}
于 2012-08-24T22:02:24.800 回答