Как встроить представления ECTJS в 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;

Я создал следующий файл 'thorne.html.ect' в каталоге '/app/views/rose'

<!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' в папке 'initializers'. Файл как ниже:

    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 в locomotive JS.

Спасибо

1 ответ

Похоже, что ваша конфигурация в 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

Другие вопросы по тегам