【问题标题】:a server side mustache.js example using node.js使用 node.js 的服务器端 mustache.js 示例
【发布时间】:2011-01-30 09:50:54
【问题描述】:

我正在寻找使用MustachejsNodejs 的示例

这是我的示例,但它不起作用。 Mustache 未定义。 我正在使用 master 分支中的 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);

【问题讨论】:

    标签: node.js serverside-javascript


    【解决方案1】:

    看看A Gentle Introduction to Node.js

    为了解决这个问题,我打开了 mustache.js 并在创建 Mustache 时删除了 var 声明

    【讨论】:

      【解决方案2】:

      我通过 npm 安装 mustache,使用正确的 require 语法并(如 Derek 所说)将 mustache 用作对象而不是函数,从而使您的示例正常工作

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

      【讨论】:

        【解决方案3】:

        感谢 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];
        

        不完全确定它在做什么,但它确实有效。现在将尝试理解它。

        【讨论】:

        【解决方案4】:

        你的例子几乎是正确的。 Mustache 是一个对象,而不是一个函数,所以它不需要 ()。改写为

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

        会让它更快乐。

        【讨论】:

          猜你喜欢
          • 2021-03-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-11-12
          • 1970-01-01
          • 1970-01-01
          • 2017-03-18
          相关资源
          最近更新 更多