RandomEtc / ejs-locals

Express 3.x layout, partial and block template functions for the EJS template engine.
298 stars 63 forks source link

<%- include('header.ejs') %> doesn't work #17

Closed ghost closed 11 years ago

ghost commented 11 years ago

The syntax

<%- include('header.ejs') %> 

doesn't work here and throws the error:

Error: ENOENT, no such file or directory '/home/boris/express/views/('header.ejs')'

while the

 <%- include header %>

is fine.

RandomEtc commented 11 years ago

Thanks. I've been seeing this too and I'm having trouble tracking it down. Do the tests work for you? (Do npm install --dev and npm test to try).

ghost commented 11 years ago

@RandomEtc The tests of include fail too.

․․․․․․․․․․․․․․․․․․․․․․․․․․

✖ 7 of 26 tests failed:

1) app GET /with-include-here should include and interpolate locals.ejs when rendering with-include.ejs:
AssertionError: expected response code of 200 'OK', but got 500 'Internal Server Error'
at Object.Assertion.status (/opt/node.js/lib/node_modules/should/lib/should.js:627:10)
at /home/boris/ejs-locals/test/test.partials.js:272:27
at IncomingMessage.Request.end (/home/boris/ejs-locals/test/support/http.js:91:9)
at IncomingMessage.EventEmitter.emit (events.js:123:20)
at IncomingMessage._emitEnd (http.js:366:10)
at HTTPParser.parserOnMessageComplete [as onMessageComplete] (http.js:149:23)
at Socket.socketOnData [as ondata] (http.js:1366:20)
at TCP.onread (net.js:403:27)

2) app GET /with-include-there should include and interpolate locals.ejs when rendering with-include.ejs:
AssertionError: expected response code of 200 'OK', but got 500 'Internal Server Error'
at Object.Assertion.status (/opt/node.js/lib/node_modules/should/lib/should.js:627:10)
at /home/boris/ejs-locals/test/test.partials.js:284:27
at IncomingMessage.Request.end (/home/boris/ejs-locals/test/support/http.js:91:9)
at IncomingMessage.EventEmitter.emit (events.js:123:20)
at IncomingMessage._emitEnd (http.js:366:10)
at HTTPParser.parserOnMessageComplete [as onMessageComplete] (http.js:149:23)
at Socket.socketOnData [as ondata] (http.js:1366:20)
at TCP.onread (net.js:403:27)

3) app GET /with-include-chain should include and interpolate include-chain-2.ejs when rendering with-include-chain.ejs:
AssertionError: expected response code of 200 'OK', but got 500 'Internal Server Error'
at Object.Assertion.status (/opt/node.js/lib/node_modules/should/lib/should.js:627:10)
at /home/boris/ejs-locals/test/test.partials.js:296:27
at IncomingMessage.Request.end (/home/boris/ejs-locals/test/support/http.js:91:9)
at IncomingMessage.EventEmitter.emit (events.js:123:20)
at IncomingMessage._emitEnd (http.js:366:10)
at HTTPParser.parserOnMessageComplete [as onMessageComplete] (http.js:149:23)
at Socket.socketOnData [as ondata] (http.js:1366:20)
at TCP.onread (net.js:403:27)

4) app GET /with-include-chain-subfolder should include and interpolate parent-include-chain.ejs when rendering with-include-chain-subfolder.ejs:
AssertionError: expected response code of 200 'OK', but got 500 'Internal Server Error'
at Object.Assertion.status (/opt/node.js/lib/node_modules/should/lib/should.js:627:10)
at /home/boris/ejs-locals/test/test.partials.js:308:27
at IncomingMessage.Request.end (/home/boris/ejs-locals/test/support/http.js:91:9)
at IncomingMessage.EventEmitter.emit (events.js:123:20)
at IncomingMessage._emitEnd (http.js:366:10)
at HTTPParser.parserOnMessageComplete [as onMessageComplete] (http.js:149:23)
at Socket.socketOnData [as ondata] (http.js:1366:20)
at TCP.onread (net.js:403:27)

5) app GET /with-two-includes should include both files and interpolate the same data:
AssertionError: expected response code of 200 'OK', but got 500 'Internal Server Error'
at Object.Assertion.status (/opt/node.js/lib/node_modules/should/lib/should.js:627:10)
at /home/boris/ejs-locals/test/test.partials.js:320:27
at IncomingMessage.Request.end (/home/boris/ejs-locals/test/support/http.js:91:9)
at IncomingMessage.EventEmitter.emit (events.js:123:20)
at IncomingMessage._emitEnd (http.js:366:10)
at HTTPParser.parserOnMessageComplete [as onMessageComplete] (http.js:149:23)
at Socket.socketOnData [as ondata] (http.js:1366:20)
at TCP.onread (net.js:403:27)

6) app GET /with-absolute-include should include locals.ejs and interpolate the data correctly:
AssertionError: expected response code of 200 'OK', but got 500 'Internal Server Error'
at Object.Assertion.status (/opt/node.js/lib/node_modules/should/lib/should.js:627:10)
at /home/boris/ejs-locals/test/test.partials.js:332:27
at IncomingMessage.Request.end (/home/boris/ejs-locals/test/support/http.js:91:9)
at IncomingMessage.EventEmitter.emit (events.js:123:20)
at IncomingMessage._emitEnd (http.js:366:10)
at HTTPParser.parserOnMessageComplete [as onMessageComplete] (http.js:149:23)
at Socket.socketOnData [as ondata] (http.js:1366:20)
at TCP.onread (net.js:403:27)

7) app GET /with-absolute-sub-include should include subfolder/sublocals.ejs and include subfolder/subitem.ejs correctly:
AssertionError: expected response code of 200 'OK', but got 500 'Internal Server Error'
at Object.Assertion.status (/opt/node.js/lib/node_modules/should/lib/should.js:627:10)
at /home/boris/ejs-locals/test/test.partials.js:344:27
at IncomingMessage.Request.end (/home/boris/ejs-locals/test/support/http.js:91:9)
at IncomingMessage.EventEmitter.emit (events.js:123:20)
at IncomingMessage._emitEnd (http.js:366:10)
at HTTPParser.parserOnMessageComplete [as onMessageComplete] (http.js:149:23)
at Socket.socketOnData [as ondata] (http.js:1366:20)
at TCP.onread (net.js:403:27)
RandomEtc commented 11 years ago

OK this is helpful - thanks! Can you let me know what OS, OS version and node version you're using?

ghost commented 11 years ago

@RandomEtc I am using Ubuntu 12.10 beta 3 and uname -a outputs

Linux tales 3.5.0-15-generic #23-Ubuntu SMP Mon Sep 24 20:37:06 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux

And my node version is v0.8.11.

dominicbarnes commented 11 years ago

I'm experiencing the same problem on my system, the output of uname -a for me is:

Linux barnes-workstation 3.5.0-17-generic #27-Ubuntu SMP Fri Oct 5 01:35:26 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux

Node Version: v0.8.11 and Express Version: 3.0.0rc5

dominicbarnes commented 11 years ago

console.log(include.toString()); outputs function () { [native code] }

If I use <%- locals.include("flash"); %> or <%- include flash %> it appears to work, as opposed to what's expected from the documentation.

RandomEtc commented 11 years ago

Weird but good to know, thanks for the sleuthing. I'll take a look later today for sure. As Express 3 settles down this is to be expected I guess. I'll also try to tackle the include and partial paths and make sure the caching is sane on those as well.

RandomEtc commented 11 years ago

Didn't get as far as I like with this, sorry! Next chance I'll have to fix it is probably Sunday.

lonevan commented 11 years ago

got the same error..

mac os 10.8.2

RandomEtc commented 11 years ago

For those following along with this - unfortunately EJS specifies its own include now!

The syntax is slightly different. Where you did <%-include('foo')%> with ejs-locals you should be able to do <% include foo %> with EJS itself.

I'll remove include from this package and publish a new version shortly. The version number will be a major increment. In the meantime I'll publish a patch fix specifying the last version of EJS that didn't have include support.

Sorry for the delay tracking this down!

RandomEtc commented 11 years ago

OK, this is "fixed" for the short-term in ejs-locals 0.2.6 which requires ejs 0.7 now. I will shortly push ejs-locals 1.0 which removes its own include so you can use the EJS one.

SV96 commented 4 years ago

<%- include('header.ejs') %> //ignores header file

<%- include header %> //Show down error

SyntaxError: Unexpected token '/' in C:\ExpressJS\RestRouteInfo\vDone1\views\register.ejs while compiling ejs

If the above error is not helpful, you may want to try EJS-Lint: https://github.com/RyanZim/EJS-Lint Or, if you meant to create an async function, pass async: true as an option. at new Function () at Template.compile (C:\ExpressJS\RestRouteInfo\vDone1\node_modules\ejs\lib\ejs.js:626:12) at Object.compile (C:\ExpressJS\RestRouteInfo\vDone1\node_modules\ejs\lib\ejs.js:366:16) at handleCache (C:\ExpressJS\RestRouteInfo\vDone1\node_modules\ejs\lib\ejs.js:215:18) at tryHandleCache (C:\ExpressJS\RestRouteInfo\vDone1\node_modules\ejs\lib\ejs.js:254:16) at View.exports.renderFile [as engine] (C:\ExpressJS\RestRouteInfo\vDone1\node_modules\ejs\lib\ejs.js:459:10) at View.render (C:\ExpressJS\RestRouteInfo\vDone1\node_modules\express\lib\view.js:135:8) at tryRender (C:\ExpressJS\RestRouteInfo\vDone1\node_modules\express\lib\application.js:640:10) at Function.render (C:\ExpressJS\RestRouteInfo\vDone1\node_modules\express\lib\application.js:592:3) at ServerResponse.render (C:\ExpressJS\RestRouteInfo\vDone1\node_modules\express\lib\response.js:1012:7) at C:\ExpressJS\RestRouteInfo\vDone1\app.js:309:9 at Layer.handle [as handle_request] (C:\ExpressJS\RestRouteInfo\vDone1\node_modules\express\lib\router\layer.js:95:5) at next (C:\ExpressJS\RestRouteInfo\vDone1\node_modules\express\lib\router\route.js:137:13) at Route.dispatch (C:\ExpressJS\RestRouteInfo\vDone1\node_modules\express\lib\router\route.js:112:3) at Layer.handle [as handle_request] (C:\ExpressJS\RestRouteInfo\vDone1\node_modules\express\lib\router\layer.js:95:5) at C:\ExpressJS\RestRouteInfo\vDone1\node_modules\express\lib\router\index.js:281:22

Liniik commented 4 years ago

I have exactly same problem as @SV96, nothing from docs or the internet works.

Frkchs commented 4 years ago

Same problem as @Liniik and @SV96! I have replaced my template engine from:

app.engine('html', require('ejs').renderFile);

to:

app.engine('html', require('ejs-locals'));

Now, the include function, work successful for me!

MadProgrammer0 commented 4 years ago

To anyone facing that problem, the new syntax Is <%- include('partials/header') %>.

Liniik commented 4 years ago

To anyone facing that problem, the new syntax Is <%- include('partials/header') %>.

Did you read my comment? I said that nothing from docs or the internet works so neither does your syntax.

I got it finally worked with <% include foot %>. My template engine was defined by express generator: app.set('view engine', 'ejs');

MadProgrammer0 commented 4 years ago

I totally forgot to mention it, so sorry for that.

medaminefh commented 4 years ago

sooo ... anyone get it right ?

Ashma-Garg commented 4 years ago

You can use

<%- include('partial/head'); %>
        <p>homepage</p>
<%- include('partial/foot'); %>

This works perfectly fine. **Don't forget to put the "semicolon" at the end of brackets. My template engine was defined by: app.set('view engine', 'ejs');

fangqiank commented 4 years ago

<%- include('partial/head'); %>

works for me,thanks