1

I'm trying to create a simple application to compile mustache templates into static pages server side, here's what I've got so far:

var view = {

    title: "Joe",
    calc: function () {

        return 2+4;
    }

};

var mustache = require("mustache");
var template = require("./home.template");

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

console.log(output);

And my template looks like:

{{title}} spend {{calc}}

Any suggestions as to what is causing this to fail?

Here is the complete error message:

home.template:1
} spend {{calc}}
        ^

module.js:437
  var compiledWrapper = runInThisContext(wrapper, filename, true);
                        ^
SyntaxError: Unexpected token {
    at Module._compile (module.js:437:25)
    at Object.Module._extensions..js (module.js:467:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.require (module.js:362:17)
    at require (module.js:378:17)
    at Object.<anonymous> (/Users/MorehouseJ09/Documents/production_development/mustache/current/compiler.js:12:16)
    at Module._compile (module.js:449:26)
    at Object.Module._extensions..js (module.js:467:10)
    at Module.load (module.js:356:32)

Any help would be great!

4

1 回答 1

2

用于fs.readFile()以字符串形式读取您的模板。Require 将不起作用,除非它需要 javascript 代码,而不是 mustache 代码。

http://nodejs.org/api/fs.html#fs_fs_readfile_filename_encoding_callback

编辑
看看这是否有效......

var mustache = require("mustache");
var fs = require("fs");

var view = {

    title: "Joe",
    calc: function () {

        return 2+4;
    }

};

fs.readFile('./home.template', 'utf-8', function (err, data) {
  if (err) throw err;
  var output = mustache.to_html(data, view);
  console.log(output);
});
于 2012-11-01T17:23:46.560 回答