How to embed ECTJS views in LocomotiveJS

571 views Asked by At

I have typical installation of locomotive JS. I want to use ECTJS for views. I have successfully installed ECTJS by using following command:

npm install ect --save

I have following controller:

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

var roseController  = new Controller();

roseController.thorne = function(){

     this.render();
}

module.exports = roseController;

I have created following 'thorne.html.ect' file in directory '/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>

I have made changes in '02_views.js' file in 'initializers' folder. File is as below:

    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' }); */
}

When I run

http://localhost:3000/rose/thorne

I get following error :

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

If I use:

this.res.send('Test');

in rose/thorne action, it shows without any problem.

Can some one guide me how can I embed ECTJS in locomotiveJS.

Thanks

1

There are 1 answers

0
Bogdan Le On

It seems that you configuration in 02_views.js is broken.

Try the following configuration:

// 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'
});

With this configuration my view files are named filename.ect. So if filename.html.ect doesnt work you can rename it to filename.ect