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

Support absolute paths for layout and partials properties for Handlebars #413

Open
2 tasks done
ludovicm67 opened this issue Feb 19, 2024 · 2 comments
Open
2 tasks done

Comments

@ludovicm67
Copy link

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the feature has not already been requested

🚀 Feature Proposal

Allow the use of absolute paths to configure the root, the layout and the options.partials properties when Handlebars is used.

Motivation

The files on the file system are like this:

views/
  layouts/
    main.hbs
  home.hbs

Right now, when I try to use absolute paths for layout, the app crashes with the following error:

Error: unable to access template "/absolute/path/to/views/layouts/main.hbs"

If I cat that exact same path, I'm able to see my template file.
So there is an issue.

I have a use-case where I need to allow a user to configure the full path of the layout, the path of the templates and the paths to some partials and the views independently.

Example

This is a basic example of code that is currently not supported:

import { dirname, join } from 'path'
import { fileURLToPath } from 'url'
import fastify from 'fastify'
import fastifyView from '@fastify/view'
import Handlebars from 'handlebars'

const currentDir = dirname(fileURLToPath(import.meta.url))

const server = fastify({
  logger: true,
})

server.register(fastifyView, {
  engine: {
    handlebars: Handlebars
  },
  root: join(currentDir, 'views'),
  viewExt: 'hbs',
  includeViewExtension: true,
  layout: join(currentDir, 'views', 'layouts', 'main.hbs'),
  defaultContext: {
    title: 'My App'
  },
})

Maybe I'm doing something wrong? I'm also open to any suggestion to get this working.

@ludovicm67
Copy link
Author

I created a simple repository to let you reproduce the issue: https://github.com/ludovicm67/bug-fastify-point-of-view-absolute-paths

@mweberxyz
Copy link
Contributor

I've been knee-deep in refactoring this plugin for about a week now so the implementation details are fresh in my mind, if I push the implementation of this feature to my fork could you help out with test coverage @ludovicm67?

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

2 participants