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

Add support for layout in handlebars #136

Merged
merged 7 commits into from Oct 31, 2019
Merged

Conversation

francisbrito
Copy link
Contributor

Relates to #112

A few notes regarding this PR:

  • It seems some templating engines already support layouts, such as pug and ejs-mate therefore I made this PR to only account for handlebars, but I'm willing to adapt it to work with other engines (that don't already support layout) if it make sense.
  • It includes a fix to TypeScript definitions.
  • It relies on a higher-order-function to enable layout support. I'm not sure what are the views on functional programming for this project, so please let me know if you'd prefer a different implementation.

Checklist

  • run npm run test and npm run benchmark
  • tests and/or benchmarks are included
  • documentation is changed or added
  • commit message and code follows Code of conduct

index.js Outdated

if (layoutFileName) {
try {
readFileSync(join(templatesDir, getPage(layoutFileName, 'hbs')))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this just a check on access?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. If an user passes a layout but the layout is not readable, this would return an error to them.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem.

index.js Outdated Show resolved Hide resolved
@@ -398,6 +409,30 @@ function fastifyView (fastify, opts, next) {
} else {
next()
}

function withLayout (render) {
if (layoutFileName) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is this option going to affect other engines?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently it would only affect handlebars. This is because some engines already support their own layouts (e.g: pug).

It is also possible to add support to other engines that don't already support layouts by applying them the withLayout decorator (see here)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would you mind throwing if layout is present and the engine is not 'handlebars'?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem. Please see my new commits.

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@mcollina
Copy link
Member

@smartiniOnGitHub would you mind to take a look?

@smartiniOnGitHub
Copy link
Contributor

@mcollina sure, I'll provide a feedbask ASAP

Copy link
Contributor

@smartiniOnGitHub smartiniOnGitHub left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, changes looks good, but in your fork I don't see last changes from original repo (11 October), could you realign your fork to have them ? Sorry for the request ... so I can retry even the execution of examples there.

@francisbrito
Copy link
Contributor Author

Hi, changes looks good, but in your fork I don't see last changes from original repo (11 October), could you realign your fork to have them ? Sorry for the request ... so I can retry even the execution of examples there.

No problem

Copy link
Contributor

@smartiniOnGitHub smartiniOnGitHub left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All good now: just done some tests even on example custom commands and all works, great job !!

@Eomm Eomm merged commit 69346eb into fastify:master Oct 31, 2019
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

Successfully merging this pull request may close these issues.

None yet

4 participants