0

Backbone を使用するプロジェクトに JST テンプレートを使用しようとしています。すべて問題なく動作しているように見えますが、理解できない構文エラーが発生しています。

コンソールの出力は次のとおりです。

Running "jst:compile" (jst) task
>> SyntaxError: Unexpected token =
Warning: JST failed to compile. Use --force to continue.

Aborted due to warnings.

--verbose を指定して実行

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.

これは、コンパイルしようとしているファイルです。

<!-- 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 プロジェクトであるため、別の方法を見つける必要がありました。やるかやらないか。

1回目の編集:

いくつかのフィードバックを得た後、次のコードを使用して、最も単純なものに分解してみました。

<% if(true){ %>
  <div>Hey</div>
<% } %>

コンパイルに成功し、これが得られます:

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};

そして、これはコンパイルに失敗します:

<div><%= this.id; %></div>
4

2 に答える 2

1

これに遭遇した他の人にとって、答えはコメントで参照されているgithubの問題にあります。[github.com/gruntjs/grunt-contrib-jst/issues/29] 答えは:

_.template と同じようにテンプレートを解析したい場合は、templateSettings を上書きしてはなりません。

だから私はこれを変更しました:

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

これに:

      jst: {
        compile: {
          options: {

          },
          files: {
            "<%= buildDir %>/templates.js": ["source.html"]
          }
        }
      }
于 2013-08-20T11:19:41.703 に答える
0

私はあなたのエラーを見つけたと思います:

<!-- IDEA START -->
<div class="ideadata" data-id="<%= id %>"

そうではない<%= this.id %>でしょうか?見つからない場合はid空になるため、 open=が発生し、エラーが発生する可能性がありますか?

于 2013-06-28T20:34:48.723 に答える