Рендеринг усов на стороне сервера

Я пытаюсь создать простое приложение для компиляции шаблонов усов в сторону статических страниц сервера, вот что я получил до сих пор:

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

И мой шаблон выглядит так:

{{title}} spend {{calc}}

Какие-нибудь предложения относительно того, что вызывает это, терпит неудачу?

Вот полное сообщение об ошибке:

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)

Любая помощь будет отличной!

1 ответ

Использование fs.readFile() читать ваш шаблон в виде строки. Require не будет работать, если не требуется код JavaScript, а не код усов.

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);
});
Другие вопросы по тегам