如何在 LocomotiveJS 中嵌入 ECTJS 视图



我有典型的机车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>

我已经在"初始值设定项"文件夹中的"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');

在罗斯/索恩的动作中,它毫无问题地显示出来。

有人可以指导我如何将 ECTJS 嵌入机车 JS。

谢谢

看来您在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

相关内容

  • 没有找到相关文章

最新更新