如何在LocomotiveJS中嵌入ECTJS视图

时间:2013-12-19 05:09:54

标签: node.js express locomotivejs

我有典型的机车JS安装。我想将ECTJS用于视图。我已使用以下命令成功安装了ECTJS:

npm install ect --save

我有以下控制器:

var locomotive = require('locomotive')
, Controller = locomotive.Controller;

var roseController  = new Controller();

roseController.thorne = function(){

     this.render();
}

module.exports = roseController;

我在目录'/ app / views / rose'

中创建了以下'thorne.html.ect'文件
<!DOCTYPE html>
<html>
<head>
    <title>Title</title>
    <link rel="stylesheet" href="/stylesheets/screen.css" />
</head>
<body>
<h1>Rose Controller - Thorne Action  from ECT</h1>
<p></p>
</body>
</html>

我在'initializers'文件夹中的'02_views.js'文件中进行了更改。文件如下:

    module.exports = function() {
  // Configure view-related settings.  Consult the Express API Reference for a
  // list of the available [settings](http://expressjs.com/api.html#app-settings).

    var ECT = require('ect');
    var renderer = ECT({ root : __dirname + '/app/views', ext : '.ect' });
    //var renderer = ECT({ watch: true, root: __dirname + '/app/views'});

    this.set('view engine', 'ect');
    this.engine('ect', renderer.render);


    // // this.set('views', __dirname + '/../../app/views');
  // // this.set('view engine', 'ejs');

  // Register EJS as a template engine.
  // //this.engine('ejs', require('ejs').__express);

  // Override default template extension.  By default, Locomotive finds
  // templates using the `name.format.engine` convention, for example
  // `index.html.ejs`  For some template engines, such as Jade, that find
  // layouts using a `layout.engine` notation, this results in mixed conventions
  // that can cause confusion.  If this occurs, you can map an explicit
  // extension to a format.
  /* this.format('html', { extension: '.jade' }) */

  // Register formats for content negotiation.  Using content negotiation,
  // different formats can be served as needed by different clients.  For
  // example, a browser is sent an HTML response, while an API client is sent a
  // JSON or XML response.
  /* this.format('xml', { engine: 'xmlb' }); */
}

当我跑步时

http://localhost:3000/rose/thorne

我收到以下错误:

500 Error: Failed to lookup view "rose/thorne.html.ect"

如果我使用:

this.res.send('Test');

在rose / thorne动作中,它没有任何问题。

有人可以指导我如何将ECTJS嵌入locomotiveJS。

谢谢

1 个答案:

答案 0 :(得分:0)

似乎您在02_views.js中的配置已损坏。

尝试以下配置:

// Creating ECT renderer
var ectRenderer = ECT({
    watch: false,
    gzip: true,
    root: __dirname + '/../../app/views'
});

// Register ECT as a template engine.
this.engine('.ect', ectRenderer.render);

// Configure application settings.  Consult the Express API Reference for a
// list of the available [settings](http://expressjs.com/api.html#app-settings).
this.set('views', __dirname + '/../../app/views');
this.set('view engine', 'ect');

// Override default template extension.  By default, Locomotive finds
// templates using the `name.format.engine` convention, for example
// `index.html.ect`  For some template engines, such as ECT, that find
// layouts using a `layout.engine` notation, this results in mixed conventions
// that can cuase confusion.  If this occurs, you can map an explicit
// extension to a format.
this.format('html', {
    extension: '.ect'
});

使用此配置,我的视图文件名为filename.ect。因此,如果filename.html.ect无效,您可以将其重命名为filename.ect