首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Grunt未能编译JST

Grunt未能编译JST
EN

Stack Overflow用户
提问于 2013-06-28 15:34:20
回答 2查看 1.3K关注 0票数 0

我试图在使用主干的项目中使用JST模板。一切似乎都正常,但我得到了一个语法错误,我无法理解。

这就是控制台输出的内容:

代码语言:javascript
复制
Running "jst:compile" (jst) task
>> SyntaxError: Unexpected token =
Warning: JST failed to compile. Use --force to continue.

Aborted due to warnings.

以--冗长的方式运行

代码语言:javascript
复制
Verifying property jst.compile exists in config...OK
Files: templates/ideas/idea.jst -> scripts/templates/ideas/idea.js
Files: templates/ideas/idea_index.jst -> scripts/templates/ideas/idea_index.js
Options: namespace="JST", templateSettings={"interpolate":{}}, processContent=undefined, separator="\n\n", prettify, processName=undefined
Reading templates/ideas/idea.jst...OK
>> SyntaxError: Unexpected token =
Warning: JST failed to compile. Use --force to continue.

Aborted due to warnings.

这是它正在试图编译的文件:

代码语言:javascript
复制
<!-- IDEA START -->
<div class="ideadata" data-id="<%= id %>" data-date="<%= this.date %>" data-votes="<%= this.votes %>">

  <!-- Id -->
  <span class="id">#<%= this.id %></span>

  <!-- Idea -->
  <p><%= this.content %></p>

  <!-- Voting and social options -->
  <div class="social">
    <!-- VOTER -->
      <!-- HTML -->
    <!-- END -->

    <a href="#" onclick="window.open(
      'https://twitter.com/intent/tweet?original_referer=' + encodeURIComponent(location.href) + '&screen_name=roskildefestival&text=<%= this.content %>&tw_p=tweetbutton', 
      'twitter-share-dialog', 
      'width=550,height=390');
        return false;"><img src="http://cdn3.iconfinder.com/data/icons/picons-social/57/03-twitter-20.png" /></a>

    <a href="#" onclick="window.open(
      'https://www.facebook.com/sharer/sharer.php?u=' + encodeURIComponent(location.href), 
      'facebook-share-dialog', 
      'width=626,height=436');
        return false;"><img src="http://cdn3.iconfinder.com/data/icons/wpzoom-developer-icon-set/500/01-20.png" /></a>
  </div>

</div>
<!-- IDEA END -->

我尝试用<%替换<%=,这似乎是成功的,但是这不是我想要实现的。

我在这里是不是做错了什么,还是这不是编写JST模板的正确方式?

我以前在Rails上运行时使用*.jst.eco作为模板文件,但是由于这是一个PHP项目,我需要想出另一种方法来做一些事情,因此也不确定这是否是最好的方法。

第一版编辑:

因此,在得到一些反馈后,我尝试用以下代码将其分解为最简单的代码:

代码语言:javascript
复制
<% if(true){ %>
  <div>Hey</div>
<% } %>

它成功地编译并给了我以下内容:

代码语言:javascript
复制
this["JST"] = this["JST"] || {};

this["JST"]["ideas/idea"] = function(obj) {obj || (obj = {});var __t, __p = '', __e = _.escape, __j = Array.prototype.join;function print() { __p += __j.call(arguments, '') }with (obj) { if(true){ ;__p += '\n  <div>Hey</div>\n'; } ;}return __p};

而这个,不能编译:

代码语言:javascript
复制
<div><%= this.id; %></div>
EN

回答 2

Stack Overflow用户

发布于 2013-08-20 11:19:41

对于其他遇到这种情况的人,答案是评论中引用的github问题。github.com/gruntjs/grunt-contrib-jst/issues/29的答案是:

如果您想像_.template一样解析模板,那么就不能覆盖templateSettings。

所以我改变了这个:

代码语言:javascript
复制
      jst: {
        compile: {
          options: {
             templateSettings: {
               interpolate : /\{\{(.+?)\}\}/g
             }
          },
          files: {
            "<%= buildDir %>/templates.js": ["source.html"]
          }
        }
      }

对此:

代码语言:javascript
复制
      jst: {
        compile: {
          options: {

          },
          files: {
            "<%= buildDir %>/templates.js": ["source.html"]
          }
        }
      }
票数 1
EN

Stack Overflow用户

发布于 2013-06-28 20:34:48

我想我发现了你的错误:

代码语言:javascript
复制
<!-- IDEA START -->
<div class="ideadata" data-id="<%= id %>"

不是应该是<%= this.id %>吗?如果它找不到id,那么它将是空的,因此您将有一个打开的=,这可能会导致错误?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17368445

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档