使用node.js的服务器端mustache.js示例

时间:2021-09-05 15:54:28

I'm looking for an example using mustache.js with node.js

我正在寻找使用带有node.js的mustache.js的示例

here is my example but it is not working. Mustache is undefined. I'm using Mustache.js from the master branch.

这是我的例子,但它不起作用。小胡子是未定义的。我正在使用master分支中的Mustache.js。

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 个解决方案

#1


29  

I got your example working by installing mustache via npm, using the correct require syntax and (as Derek said) using mustache as an object not a function

我通过npm安装胡子,使用正确的require语法和(如Derek所说)使用胡子作为对象而不是函数

npm install mustache

then

然后

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

#2


16  

Your example is almost correct. Mustache is an object, not a function, so it doesn't need the (). Rewritten as

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

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

will make it happier.

会让它更快乐。

#3


10  

Thanks to Boldr http://boldr.net/create-a-web-app-with-node Had to add the following code to mustache.js

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

Not exactly sure what it is doing but it works. Will try to understand it now.

不完全确定它在做什么,但它的工作原理。现在会尝试理解它。

#4


0  

Take a look to A Gentle Introduction to Node.js

看看Node.js的A Gentle Introduction

To fix I opened up mustache.js and removed the var declaration when Mustache is being created

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

#1


29  

I got your example working by installing mustache via npm, using the correct require syntax and (as Derek said) using mustache as an object not a function

我通过npm安装胡子,使用正确的require语法和(如Derek所说)使用胡子作为对象而不是函数

npm install mustache

then

然后

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

#2


16  

Your example is almost correct. Mustache is an object, not a function, so it doesn't need the (). Rewritten as

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

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

will make it happier.

会让它更快乐。

#3


10  

Thanks to Boldr http://boldr.net/create-a-web-app-with-node Had to add the following code to mustache.js

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

Not exactly sure what it is doing but it works. Will try to understand it now.

不完全确定它在做什么,但它的工作原理。现在会尝试理解它。

#4


0  

Take a look to A Gentle Introduction to Node.js

看看Node.js的A Gentle Introduction

To fix I opened up mustache.js and removed the var declaration when Mustache is being created

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