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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2493460/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 13:37:47  来源:igfitidea点击:

a server side mustache.js example using node.js

node.jsserverside-javascript

提问by onecoder4u

I'm looking for an example using Mustachejswith Nodejs

我正在寻找一个使用Mustachejswith的例子Nodejs

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

这是我的示例,但它不起作用。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);

回答by AngusC

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 安装 mustache,使用正确的 require 语法和(如 Derek 所说)使用 mustache 作为对象而不是函数来使您的示例工作

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

回答by Derek Gathright

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.

会让它更快乐。

回答by onecoder4u

Thanks to Boldr http://boldr.net/create-a-web-app-with-nodeHad 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.

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

回答by Pietro

Take a look to A Gentle Introduction to Node.js

看一看Node.js 的温柔介绍

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

为了修复,我打开了 mustache.js 并在创建 Mustache 时删除了 var 声明