29

Mustachejsを使用した例を探していますNodejs

これが私の例ですが、機能していません。Mustache未定義です。マスターブランチのMustachejsを使用しています。

var sys = require('sys');
var m = require("./mustache");

var view = {
  title: "Joe",
  calc: function() {
    return 2 + 4;
  }
};    
var template = "{{title}} spends {{calc}}";    
var html = Mustache().to_html(template, view);

sys.puts(html);
4

4 に答える 4

33

npm経由で口ひげをインストールし、正しいrequire構文を使用し、(デレクが言ったように)口ひげを関数ではなくオブジェクトとして使用することで、あなたの例を機能させました

npm install mustache

それから

var sys = require('sys');
var mustache = require('mustache');

var view = {
  title: "Joe",
  calc: function() {
    return 2 + 4;
  }
};

var template = "{{title}} spends {{calc}}";

var html = mustache.to_html(template, view);

sys.puts(html); 
于 2011-11-12T18:33:36.243 に答える
18

あなたの例はほぼ正しいです。Mustache は関数ではなくオブジェクトなので、() は必要ありません。と書き直した

var html = Mustache.to_html(template, view);

より幸せにします。

于 2010-11-06T20:40:11.457 に答える
10

Boldr に感謝http://boldr.net/create-a-web-app-with-node 次のコードを mustache.js に追加する必要がありました

for (var name in Mustache)
    if (Object.prototype.hasOwnProperty.call(Mustache, name))
        exports[name] = Mustache[name];

それが何をしているのか正確にはわかりませんが、動作します。今それを理解しようとします。

于 2010-03-22T17:13:09.633 に答える
-2

A Gentle Introduction to Node.js をご覧ください

修正するために、mustache.js を開いて、Mustache の作成時に var 宣言を削除しました。

于 2012-01-11T09:30:14.683 に答える