如何在其中包含Django模板标记的文件上运行JSHint? [英] How to run JSHint on files with Django template markup in them?

查看:117
本文介绍了如何在其中包含Django模板标记的文件上运行JSHint?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的所有Javascript源文件上运行JSHint,但其中一些有一些嵌入式Django模板标记。 JSHint在这个标记上引发了大量错误。

I would like to run JSHint on all my Javascript source files but several of them have some embedded Django template markup. JSHint throws a ton of errors on this markup.

有没有办法可以......

Is there a way to either...


  1. 告诉JSHint忽略这个标记

  2. 运行带有一些虚拟数据的Djnago模板解析器来生成渲染的js文件的所有排列,然后运行JSHint?

我假设我可以编写一堆代码来做#2,但我想知道是否有更简单的方法。

I assume I could write a bunch of code to do #2 but i'm wondering if there's an easier way.

推荐答案

根据标记,您可以通过JavaScript注释隐藏Django标记。我们这样做,例如:

Depending on a markup you can get away with "hiding" Django markup with JavaScript comments. We do this, for example:

// {% if cond %}
someJavaScriptCode();
// {% else %}
somethingElse();
// {% endif %}

// {% include "script.js" %}

唯一的事情就是你用一个空白行启动script.js - 否则 // 会吃第一个合法的JavaScript行代码你在那里。我写了一个简单的帮助 {%includejs%} ,它会自动为我做这件事。

The only thing is that you have start script.js with a blank line—otherwise // will eat the first legitimate line of JavaScript code you have there. I wrote a simple helper {% includejs %} that does that for me automatically.

这篇关于如何在其中包含Django模板标记的文件上运行JSHint?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆