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 include view amid compiling #594

Closed
tgbv opened this issue Apr 2, 2021 · 3 comments
Closed

Cannot include view amid compiling #594

tgbv opened this issue Apr 2, 2021 · 3 comments

Comments

@tgbv
Copy link

tgbv commented Apr 2, 2021

I have my project structured like this. So simple it hurts:

/lib/ejs-template.ejs

/views/home.ejs
/views/header.ejs

Inside home.ejs I must include header.ejs. I am mercifully following their documentation: https://ejs.co/#docs and include the file this way because "Includes are relative to the template with the include call",

<%- include("header") %>

Inside ejs-template.ejs I am compiling the template like this:

module.exports = (target, vars = {})=>{
    return Ejs.compile( Fs.readFileSync(`${__dirname}/../views/${target}.ejs`, {encoding: 'utf-8'}), {
        views: [ Path.resolve('../views') ],
    })(vars)
}

Yet I get this error!

Error: ejs:38
    36| <body>
    37|
 >> 38| <%- include("header") %>
    39|
    40| <main class="container">
    41|     <h4>Some stuff</h4>

Could not find the include file "header"
    at getIncludePath ([redacted]\server\node_modules\ejs\lib\ejs.js:183:13)
    at includeFile ([redacted]\server\node_modules\ejs\lib\ejs.js:309:19)
    at include ([redacted]\server\node_modules\ejs\lib\ejs.js:690:16)
    at eval (eval at compile ([redacted]\server\node_modules\ejs\lib\ejs.js:662:12), <anonymous>:12:17)
    at anonymous ([redacted]\server\node_modules\ejs\lib\ejs.js:692:17)
    at module.exports ([redacted]\server\util\ejs-template.js:13:7)
    at [redacted]\server\route\site.js:13:18
    at Layer.handle [as handle_request] ([redacted]\server\node_modules\express\lib\router\layer.js:95:5)
    at next ([redacted]\server\node_modules\express\lib\router\route.js:137:13)
    at module.exports ([redacted]\server\middleware\isLoggedIn.js:8:9) {
  path: ''
}

I have set the views path in the compiling options. I have tried with/without path. I've also tried with root set at the same exact views path.

What am I missing? The documentation is lax and offers no clues regarding this.

@RyanZim
Copy link
Collaborator

RyanZim commented Apr 2, 2021

You're setting the views path with path.resolve, which will resolve relative to process.cwd(), but it looks like you're setting the views path as if it'd be relative to __dirname. Try Path.resolve(__dirname, '../views').

@tgbv
Copy link
Author

tgbv commented Apr 2, 2021

That did it! Thank you!

@tgbv tgbv closed this as completed Apr 2, 2021
@ebonyenny
Copy link

i am still having the same error but i am compiling in my app.js

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

No branches or pull requests

3 participants