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

Include issue #20

Closed
dnohr opened this issue Nov 5, 2017 · 10 comments
Closed

Include issue #20

dnohr opened this issue Nov 5, 2017 · 10 comments

Comments

@dnohr
Copy link

dnohr commented Nov 5, 2017

Hi guys,

Have any of you got troubles with including other EJS components within an EJS file? I'm getting following error "Error: Could not find include include file.".

Example:
<% include header %> within the "index.ejs" file.

It works in Express, so maybe I just can't find the relative path somehow. All my ejs files is located in the same folder, so shouldn't be an issue.

Thanks.

@delvedor
Copy link
Member

delvedor commented Nov 6, 2017

Hi! Thank your for checking out Fastify!
Can you create a complete example with a gist?

@dnohr
Copy link
Author

dnohr commented Nov 6, 2017

Hi Tomas,

For sure, check out here:
https://gist.github.com/dnohr/de935d01dc22d5cf45dee34979dda3a1

All the EJS files are located in the same "views" folder, but resulting in the same error:
"Error: Could not find include include file."

I have tried implement the EJS include function in different ways, but can't find the right relative path. This example works fine in Express, but would for sure like to test Fastify :-)

Thanks.

@delvedor
Copy link
Member

delvedor commented Nov 6, 2017

If you are using include ejs needs a filename option.

'use strict'

const fastify = require('fastify')()
const resolve = require('path').resolve

fastify.register(require('point-of-view'), {
  engine: {
    ejs: require('ejs')
  },
  options: { // this object will be passed to ejs.compile
    filename: resolve('views')
  },
  templates: 'views'
})

fastify.get('/', (req, reply) => {
  reply.view('/index.ejs', {})
})

fastify.listen(3000, (err) => {
  if (err) throw err
  console.log(`Server up: ${fastify.server.address().port}`)
})

@dnohr
Copy link
Author

dnohr commented Nov 6, 2017

Hi again,

Unfortunately the filename within the "fastify.register" options didn't work. I couldn't print the variable in the EJS file or use it in the EJS include function.

The only way to make it work is by adding the resolved path in the "reply.view", but it's not a pretty solution. Any ideas on fixing it?

JS
reply.view('index.ejs', { filename: path.resolve('views') });

EJS
<% include(filename + '/header.ejs') %>

Thanks.

@delvedor
Copy link
Member

delvedor commented Nov 6, 2017

I have answered in your gist. In that way everything works. :)

@dnohr
Copy link
Author

dnohr commented Nov 6, 2017

Super, didn't spot it. Much better!

Thanks for solving it Tomas 👍🏻

@dnohr dnohr closed this as completed Nov 6, 2017
@delvedor
Copy link
Member

delvedor commented Nov 6, 2017

You're welcome! If you have more questions or suggestions please open other issues!

@smartiniOnGitHub
Copy link
Contributor

Hi all, I spent a lot of time on the same problem ... thanks a lot for the comments in the gist.
Sorry, could you put that info even in some documentation (project README) or one of the samples ? Could be useful to many people ...
Anyway thanks a lot,
Sandro

@mcollina
Copy link
Member

Sandro, the best approach would be if you open up a PR with the doc/example you think we are missing!

@smartiniOnGitHub
Copy link
Contributor

Hi Matteo, I'll do that. Thanks for now.

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

4 participants