Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot figure out how to change template engine #3961

Closed
jhechtf opened this issue Jan 17, 2017 · 3 comments
Closed

Cannot figure out how to change template engine #3961

jhechtf opened this issue Jan 17, 2017 · 3 comments

Comments

@jhechtf
Copy link

jhechtf commented Jan 17, 2017

Sails version: 0.12.11
Node version: 6.8.1
NPM version: 3.10.8
Operating system: Windows 10


Forgive me, but do understand I do this entirely as a last resort. I tried EJS, and the basic truth of it is that I do not like it. In looking I found that ECT seems to be more applicable to me, but I cannot figure out how to properly change the view engine. All I tend to get are errors.

Things I've done

  1. Installed ECT via npm install --save ect
  2. Uninstalled EJS via npm uninstall --save ejs
  3. Renamed the *.ejs files to *.ect files.
  4. Changed the config/views.js config engine from ejs to ect
  5. Set layout:false
  6. Created a github repo containing code to test https://github.com/jhechtf/ect-test
  7. Tried to read through the error given to me in the console.

Unhandled Rejection TypeError: engine.configure is not a function at C:\Users\Jhecht\AppData\Roaming\npm\node_modules\sails\node_modules\consolidate\lib\consolidate.js:952:12 ...<whole bunch more text from here that I can copy in case you think it might be helpful>

Looking at that file/line the code I see is this: (rest of function given for context):

exports.ect = function(path, options, fn){
  return promisify(fn, function (fn) {
    var engine = requires.ect;
    if (!engine) {
      var ECT = require('ect');
      engine = requires.ect = new ECT(options);
    }
    engine.configure({ cache: options.cache }); //Line 952
    engine.render(path, options, fn);
  });
};

I have not found a guide or anything resembling one on how to change the view template (short of using sails new app --template=jade and a fix on an issue that does not seem to be relevant anymore, or at least I can't find the file it points to in order to attempt the same thing).

Any help on how to change the template engine in general would be appreciated, and I am sorry to bother the Sails team but I hope everyone understand this is my last ditch effort to figure out how to change the sails template engine to something I would prefer to use.

@daniyel
Copy link

daniyel commented Feb 5, 2017

Hi.

I am experiencing the same problem. I am using Sails version 0.12.11 and ect 0.5.9.

Problem is same and I still haven't figured out the solution.

Unhandled rejection TypeError: engine.configure is not a function
  at /usr/local/lib/node_modules/sails/node_modules/consolidate/lib/consolidate.js:952:12
  at /usr/local/lib/node_modules/sails/node_modules/consolidate/lib/consolidate.js:143:5
  at Promise._execute (/usr/local/lib/node_modules/sails/node_modules/bluebird/js/release/debuggability.js:300:9)
  at Promise._resolveFromExecutor (/usr/local/lib/node_modules/sails/node_modules/bluebird/js/release/promise.js:481:18)
  at new Promise (/usr/local/lib/node_modules/sails/node_modules/bluebird/js/release/promise.js:77:14)
  at promisify (/usr/local/lib/node_modules/sails/node_modules/consolidate/lib/consolidate.js:136:10)
  at exports.ect (/usr/local/lib/node_modules/sails/node_modules/consolidate/lib/consolidate.js:946:10)
  at SailsView.sails.config.views.engine.fn [as engine] (/usr/local/lib/node_modules/sails/lib/hooks/views/configure.js:83:7)
  at SailsView.View.render (/usr/local/lib/node_modules/sails/node_modules/express/lib/view.js:76:8)
  at Function.app.render (/usr/local/lib/node_modules/sails/node_modules/express/lib/application.js:561:10)
  at ServerResponse.res.render (/usr/local/lib/node_modules/sails/node_modules/express/lib/response.js:845:7)
  at ServerResponse.res.view (/usr/local/lib/node_modules/sails/lib/hooks/views/res.view.js:284:16)
  at serveView (/usr/local/lib/node_modules/sails/lib/hooks/views/onRoute.js:61:20)
  at routeTargetFnWrapper (/usr/local/lib/node_modules/sails/lib/router/bind.js:181:5)
  at callbacks (/usr/local/lib/node_modules/sails/node_modules/express/lib/router/index.js:164:37)
  at param (/usr/local/lib/node_modules/sails/node_modules/express/lib/router/index.js:138:11)
  at pass (/usr/local/lib/node_modules/sails/node_modules/express/lib/router/index.js:145:5)
  at nextRoute (/usr/local/lib/node_modules/sails/node_modules/express/lib/router/index.js:100:7)
  at callbacks (/usr/local/lib/node_modules/sails/node_modules/express/lib/router/index.js:167:11)
  at module.exports (/usr/local/lib/node_modules/sails/lib/hooks/cors/clear-headers.js:14:3)
  at routeTargetFnWrapper (/usr/local/lib/node_modules/sails/lib/router/bind.js:181:5)
  at callbacks (/usr/local/lib/node_modules/sails/node_modules/express/lib/router/index.js:164:37)
  at param (/usr/local/lib/node_modules/sails/node_modules/express/lib/router/index.js:138:11)
  at pass (/usr/local/lib/node_modules/sails/node_modules/express/lib/router/index.js:145:5)
  at nextRoute (/usr/local/lib/node_modules/sails/node_modules/express/lib/router/index.js:100:7)
  at callbacks (/usr/local/lib/node_modules/sails/node_modules/express/lib/router/index.js:167:11)
  at sails.router.bind._middlewareType (/usr/local/lib/node_modules/sails/lib/hooks/csrf/index.js:148:11)
  at routeTargetFnWrapper (/usr/local/lib/node_modules/sails/lib/router/bind.js:181:5)
  at callbacks (/usr/local/lib/node_modules/sails/node_modules/express/lib/router/index.js:164:37)
  at param (/usr/local/lib/node_modules/sails/node_modules/express/lib/router/index.js:138:11)
  at pass (/usr/local/lib/node_modules/sails/node_modules/express/lib/router/index.js:145:5)
  at nextRoute (/usr/local/lib/node_modules/sails/node_modules/express/lib/router/index.js:100:7)
  at callbacks (/usr/local/lib/node_modules/sails/node_modules/express/lib/router/index.js:167:11)
  at _addResViewMethod (/usr/local/lib/node_modules/sails/lib/hooks/views/res.view.js:372:3)
  at routeTargetFnWrapper (/usr/local/lib/node_modules/sails/lib/router/bind.js:181:5)
  at callbacks (/usr/local/lib/node_modules/sails/node_modules/express/lib/router/index.js:164:37)
  at param (/usr/local/lib/node_modules/sails/node_modules/express/lib/router/index.js:138:11)
  at pass (/usr/local/lib/node_modules/sails/node_modules/express/lib/router/index.js:145:5)
  at nextRoute (/usr/local/lib/node_modules/sails/node_modules/express/lib/router/index.js:100:7)
  at callbacks (/usr/local/lib/node_modules/sails/node_modules/express/lib/router/index.js:167:11)
  at /usr/local/lib/node_modules/sails/lib/hooks/i18n/index.js:35:25
  at Object.i18nInit [as init] (/usr/local/lib/node_modules/sails/node_modules/i18n/i18n.js:201:14)
  at addLocalizationMethod (/usr/local/lib/node_modules/sails/lib/hooks/i18n/index.js:33:26)
  at routeTargetFnWrapper (/usr/local/lib/node_modules/sails/lib/router/bind.js:181:5)
  at callbacks (/usr/local/lib/node_modules/sails/node_modules/express/lib/router/index.js:164:37)
  at param (/usr/local/lib/node_modules/sails/node_modules/express/lib/router/index.js:138:11)
  at pass (/usr/local/lib/node_modules/sails/node_modules/express/lib/router/index.js:145:5)
  at nextRoute (/usr/local/lib/node_modules/sails/node_modules/express/lib/router/index.js:100:7)
  at callbacks (/usr/local/lib/node_modules/sails/node_modules/express/lib/router/index.js:167:11)
  at addResponseMethods (/usr/local/lib/node_modules/sails/lib/hooks/responses/index.js:149:18)
  at routeTargetFnWrapper (/usr/local/lib/node_modules/sails/lib/router/bind.js:181:5)
  at callbacks (/usr/local/lib/node_modules/sails/node_modules/express/lib/router/index.js:164:37)
  at param (/usr/local/lib/node_modules/sails/node_modules/express/lib/router/index.js:138:11)
  at pass (/usr/local/lib/node_modules/sails/node_modules/express/lib/router/index.js:145:5)
  at nextRoute (/usr/local/lib/node_modules/sails/node_modules/express/lib/router/index.js:100:7)
  at callbacks (/usr/local/lib/node_modules/sails/node_modules/express/lib/router/index.js:167:11)
  at /usr/local/lib/node_modules/sails/lib/hooks/request/index.js:122:13
  at /usr/local/lib/node_modules/sails/node_modules/connect-flash/lib/flash.js:21:5
  at addMixins (/usr/local/lib/node_modules/sails/lib/hooks/request/index.js:100:11)
  at routeTargetFnWrapper (/usr/local/lib/node_modules/sails/lib/router/bind.js:181:5)
  at callbacks (/usr/local/lib/node_modules/sails/node_modules/express/lib/router/index.js:164:37)
  at param (/usr/local/lib/node_modules/sails/node_modules/express/lib/router/index.js:138:11)
  at pass (/usr/local/lib/node_modules/sails/node_modules/express/lib/router/index.js:145:5)
  at Router._dispatch (/usr/local/lib/node_modules/sails/node_modules/express/lib/router/index.js:173:5)
  at Object.router (/usr/local/lib/node_modules/sails/node_modules/express/lib/router/index.js:33:10)
  at next (/usr/local/lib/node_modules/sails/node_modules/express/node_modules/connect/lib/proto.js:174:15)
  at Object.xPoweredBy [as handle] (/usr/local/lib/node_modules/sails/lib/hooks/http/get-configured-http-middleware-fns.js:222:7)
  at next (/usr/local/lib/node_modules/sails/node_modules/express/node_modules/connect/lib/proto.js:174:15)
  at Object.methodOverride [as handle] (/usr/local/lib/node_modules/sails/node_modules/method-override/index.js:65:14)
  at next (/usr/local/lib/node_modules/sails/node_modules/express/node_modules/connect/lib/proto.js:174:15)
  at next (/usr/local/lib/node_modules/sails/node_modules/express/node_modules/connect/lib/proto.js:176:9)
  at Object._parseHTTPBody [as handle] (/usr/local/lib/node_modules/sails/node_modules/skipper/index.js:56:14)
  at next (/usr/local/lib/node_modules/sails/node_modules/express/node_modules/connect/lib/proto.js:174:15)
  at /usr/local/lib/node_modules/sails/lib/hooks/http/get-configured-http-middleware-fns.js:104:22
  at Immediate.<anonymous> (/usr/local/lib/node_modules/sails/node_modules/express-session/index.js:473:7)
  at runCallback (timers.js:578:20)
  at tryOnImmediate (timers.js:554:5)
  at processImmediate [as _immediateCallback] (timers.js:533:5)

@daniyel
Copy link

daniyel commented Feb 6, 2017

Hi.

I found out where is the problem. Problem is in consolidate.js. On the line 948 there is var engine = requires.ect; and later it fails on the line 953 engine.configure({ cache: options.cache });, because engine is function. And when I checked ect documentation, you use it like this:

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

so that means in consolidate.js on the line 948 it should be var engine = requires.ect(); and not var engine = requires.ect;. Then it works.

Best regards.

@sgress454
Copy link
Member

Hi @jhechtf, @daniyel;

Thanks for reaching out. It looks like the ECT engine usage is a bit different than most of the other template engines supported by Consolidate, so you have to use the extended view config syntax to make it work. In config/views.js, set:

  engine: {
    ext: 'ect',
    fn: (function() {
      var ECT = require('ect');
      return (new ECT()).render;
    })()
  },

The fn part can actually be shortened to (new (require('ect'))()).render, but it's less clear what's going on when you do it that way.

A couple of things to keep in mind:

  1. It looks like ECT hasn't been actively maintained for a few years. That doesn't imply there's anything wrong with it, but you just won't necessarily find a lot of support.
  2. Issues like this are exactly why we changed the custom view engine functionality for Sails 1.0!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants