如果没有行号,调试 underscore.js 模板会很困难

发布于 2024-11-09 21:27:34 字数 193 浏览 0 评论 0原文

我正在将一个相当大的 php 模板(其中包含基本逻辑的页面)转换为 underscore.js 模板。

问题是我一直在其中出现错误,并且它的缩小“编译”版本在引发错误时不会提供有用的信息或行号。

有没有办法在 underscore.js 中获得更好的模板调试(例如行号)?如果没有,有没有办法让模板在某个点终止(这样我可以缩小错误的范围)。

I am converting a rather large php template (page with basic logic in it) into an underscore.js template.

The problem is that I keep having errors in it, and it's minified "compiled" version doesn't give useful information or line numbers when an error is thrown.

Is there a way to get better template debugging in underscore.js (such as line numbers)? And if not, is there a way to get the template to terminate at a point (this way I can narrow down where the error is).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

千鲤 2024-11-16 21:27:34

截至 2012 年 4 月的勘误表: Underscore 1.3.2(2012 年 4 月 9 日)引入了对 _.template() 的更改,请检查 changelog 和来源,因为此处描述的内容可能会出现复杂情况。

是和否 - 模板是首先翻译为一串(难以阅读)Javascript 代码,并作为一个代码块执行,因此,如果您正在查找语法错误,则必须从您尝试执行的模板中删除有问题的代码。

但如果是其他内容,请嵌入 <% return __p.join(''); %> 将中断执行并返回模板的结果(阅读源代码以了解原因,但本质上,模板块的结果被放入名为 __p 一个接一个)。

您还可以在模板评估时进行日志记录(即在模板中放入 <% console.log(<..>) %> 来查看诊断信息。要进行更高级的故障排除,您可以还可以在模板代码中放入 <% debugger; %> 放入您最喜欢的调试器中,尽管您将看到的代码阅读起来不友好,但您将可以访问评估模板范围。如果

我正在做大量的工作并且需要更广泛的调试工具,我可能会获取 underscore.js 脚本的副本并向 _.template() 添加一些诊断支持代码。 函数本身。

_.template = function(str, data) {
  var c  = _.templateSettings;
  var tmpl = 'var __p=[],print=function(){__p.push.apply(__p,arguments);};' +
    'with(obj||{}){__p.push(\'' +
    str.replace(/\\/g, '\\\\')
       .replace(/'/g, "\\'")
       .replace(c.interpolate, function(match, code) {
         return "'," + code.replace(/\\'/g, "'") + ",'";
       })
       .replace(c.evaluate || null, function(match, code) {
         return "');" + code.replace(/\\'/g, "'")
                            .replace(/[\r\n\t]/g, ' ') + "__p.push('";
       })
       .replace(/\r/g, '\\r')
       .replace(/\n/g, '\\n')
       .replace(/\t/g, '\\t')
       + "');}return __p.join('');";
  console.log(tmpl.replace(/;/g, '\n')); // <- dump compiled code to console before evaluating
  var func = new Function('obj', tmpl);
  return data ? func(data) : func;
};

Errata as of Apr 2012: Underscore 1.3.2 (April 9, 2012) have introduced changes to _.template(), check the changelog and the source since complications to what have been described here might have shown up.

Yes and no - the template is first translated to a string of (hard to read) Javascript code and the executed as one block of code, so if you're looking for a syntax error you must remove the offending code from the template you're trying to execute.

But if it's something else, embedding a <% return __p.join(''); %> will break execution and return the result of the template up to that point (read the source to see why, but essentially, the results of template blocks are put into an array named __p one after another).

You can also do logging while your template evaluates (i.e. put <% console.log(<..>) %> in your template to see diagnostics. For more advanced troubleshooting, you could also put a <% debugger; %> in your template code to drop into your favourite debugger. Although the code you'll see will be unfriendly to read you will have access to the evaluating templates scope.

If I were doing extensive work and needed more extensive debugging facilities, I'd probably take a copy of the underscore.js script and add some diagnostic support code to the _.template() function itself. For example:

_.template = function(str, data) {
  var c  = _.templateSettings;
  var tmpl = 'var __p=[],print=function(){__p.push.apply(__p,arguments);};' +
    'with(obj||{}){__p.push(\'' +
    str.replace(/\\/g, '\\\\')
       .replace(/'/g, "\\'")
       .replace(c.interpolate, function(match, code) {
         return "'," + code.replace(/\\'/g, "'") + ",'";
       })
       .replace(c.evaluate || null, function(match, code) {
         return "');" + code.replace(/\\'/g, "'")
                            .replace(/[\r\n\t]/g, ' ') + "__p.push('";
       })
       .replace(/\r/g, '\\r')
       .replace(/\n/g, '\\n')
       .replace(/\t/g, '\\t')
       + "');}return __p.join('');";
  console.log(tmpl.replace(/;/g, '\n')); // <- dump compiled code to console before evaluating
  var func = new Function('obj', tmpl);
  return data ? func(data) : func;
};
甜心小果奶 2024-11-16 21:27:34

在 Backbone Eye(Firebug 扩展)中,您可以调试下划线模板 - 就像它们是常规 JavaScript 文件一样。模板 ID(如果指定)出现在(Firefox 的)脚本窗口中,您可以选择它(就像常规脚本文件一样),放置断点并观察模板的增量构建。有关如何执行此操作的更多详细信息,请访问 http://dhruvaray.github.io/spa-eye /#views。这应该可以帮助您轻松缩小错误来源的范围。

[免责声明:我是Backbone Eye的作者]

In Backbone Eye (Firebug extension), you can debug underscore templates - just as if they were regular JavaScript files. The template id (if specified) comes up in the script window (of Firefox) and you can select it (just like a regular script file), put breakpoints and watch the template being incrementally built. More details on how to do this is at http://dhruvaray.github.io/spa-eye/#views. This should help you narrow down the source of your error easily.

[Disclaimer : I am the author of Backbone Eye]

余生再见 2024-11-16 21:27:34

如果长模板中存在 javascript 语法错误,有时很难找到,因为 linter 无法理解 <%%> 中的 javascript。

使用编辑器替换功能并替换:

查找:

%>[\s\S]*?<%[=-]?

替换:

\n//html\n

它将删除 javascript 之间的所有 HTML,然后您可以使用 linter 或 http://esprima.org/demo/validate.html 查找语法错误。

HTML 模板

全部替换并美化后:
剩余 javascript

注意:此正则表达式将在文件的开头和结尾留下剩余的 HTML,因此只需手动删除它们即可。

If you have a javascript syntax error inside a long template sometimes it can be hard to find, since the linter will not understand javascript inside <%%>

Use your editor replace function and replace:

Find:

%>[\s\S]*?<%[=-]?

Replace:

\n//html\n

It will remove any HTML between your javascript then you can use a linter or http://esprima.org/demo/validate.html to find for syntax errors.

HTML template

After replace all and beautified:
remaining javascript

Note: This regular expression will leave remaining HTML on the start and end of your file, so just remove those manually.

总攻大人 2024-11-16 21:27:34

只需使用 Internet Explorer 开发工具控制台 (F12)。它能够调试到您的下划线模板中。我使用了 chrome 和 firefox,最多得到的是 jquery.js 或 underscore.js 中发生错误的行。但 Internet Explorer 调试器直接转到下划线模板内的有问题的代码。这将使您能够找出错误。

Simply use the Internet Explorer Development tools console(F12). It is able to debug into your underscore template. I used chrome and firefox and the most I got was the line in jquery.js or underscore.js that the error occured. But the Internet explorer debugger went directly to the offending code inside the underscore template. This will enable you to figure out the error.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文