首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在LocomotiveJS中嵌入ECTJS视图

如何在LocomotiveJS中嵌入ECTJS视图
EN

Stack Overflow用户
提问于 2013-12-19 13:09:54
回答 1查看 556关注 0票数 0

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

代码语言:javascript
复制
npm install ect --save

我有以下控制器:

代码语言:javascript
复制
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’文件

代码语言:javascript
复制
<!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’文件中进行了更改。文件如下:

代码语言:javascript
复制
    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' }); */
}

当我跑的时候

代码语言:javascript
复制
http://localhost:3000/rose/thorne

我得到以下错误:

代码语言:javascript
复制
500 Error: Failed to lookup view "rose/thorne.html.ect"

如果我使用:

代码语言:javascript
复制
this.res.send('Test');

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

谁能指导我如何在locomotiveJS中嵌入ECTJS?

谢谢

EN

回答 1

Stack Overflow用户

发布于 2014-04-05 14:09:00

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

尝试以下配置:

代码语言:javascript
复制
// 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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20673887

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档