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

发布于 2024-08-25 22:43:27 字数 421 浏览 5 评论 0原文

我正在寻找一个使用 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);

I'm looking for an example using Mustachejs with Nodejs

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

别忘他 2024-09-01 22:43:27

我通过 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); 

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 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); 
月棠 2024-09-01 22:43:27

你的例子几乎是正确的。 Mustache 是一个对象,而不是函数,因此不需要 ()。重写为

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

会让它更快乐。

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

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

will make it happier.

荭秂 2024-09-01 22:43:27

感谢 Boldr http://boldr.net/create-a-web-app-带节点
必须将以下代码添加到 Mustache.js

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

不确定它在做什么,但它可以工作。现在将尝试理解它。

Thanks to Boldr http://boldr.net/create-a-web-app-with-node
Had to add the following code to 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.

笛声青案梦长安 2024-09-01 22:43:27

查看Node.js 简介

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

Take a look to A Gentle Introduction to Node.js

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文