-
-
Notifications
You must be signed in to change notification settings - Fork 87
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
fastify.view
is rendered without reply.locals
#394
Comments
Well, a quick workaround to the problem would be to pass async handler(request, reply) {
console.log('Locals: ', reply.locals);
const html = await this.render('page', {
...reply.locals,
});
reply.type('text/html');
return html;
} But it would be much better to be implemented out of the box if possible |
Thanks for reporting! Would you like to send a Pull Request to address this issue? Remember to add unit tests. |
As I was reading the source code, I found the difference lies behind the decoration in Fastify. When a reply is decorated with "view" and "locals", they are in the same Reply class, but that's not the case for decoration of Fastify instance with "view". So, probably, this issue should be resolved in Fastify, but I'm not a big fan of coupling two packages together this way. I see a possibility to get Fastify's "Reply Symbol" in this package like so: const ReplySymbol = Object.getOwnPropertySymbols(this).find((symbol) => {
return symbol.description === 'fastify.Reply';
}); And then use it to retrieve Anyway, I think Fastify's point-of-view should provide the same behaviour for UPD. |
I would be glad to help and make a pull request. Do you know if it is possible to get a |
You can't, that's the key difference between the two APIs. However, we could add a way to pass a reply in to the one on the instance. |
I was researching quite a lot about how we can get or pass an actual
fastify.get('/', (request, reply) => {
const html = await reply.view('index-for-layout.hbs', data, { renderOnly: true });
});
@mcollina, what do you and the community think about that? |
Can you explain why? |
Currently, |
I would actually prefer not to have different result for the function based on the option. Maybe add a fresh one? |
That would work. We can decorate the |
how about |
Taking into account how people use |
Another issue of current approach is the |
After #405 merges this could be solved with a documentation change. IE the following will work as expected:
Happy to create new PR with documentation change and tests after 405 merges if this is a suitable solution. |
@mweberxyz would you mind to se4nd a PR for the doc change? |
@mcollina Between this issue and #412, I think best solution (for both) would be your original direction suggested in #394 (comment) We can keep both I propose |
I like the idea of appending |
Closes fastify#394 Closes fastify#412
Closes fastify#394 Closes fastify#412
Closes fastify#394 Closes fastify#412
Closes fastify#394 Closes fastify#412
Closes fastify#394 Closes fastify#412
Closes fastify#394 Closes fastify#412
Closes fastify#394 Closes fastify#412
I didn't realize every time I force push it does this to the issue 😳, will try to catch mistakes before creating PR in future. |
Prerequisites
Fastify version
4.23.2
Plugin version
8.2.0
Node.js version
20.8.0
Operating system
macOS
Operating system version (i.e. 20.04, 11.3, 10)
14.0 (23A344)
Description
There is a possibility to render the template into a variable (as described here) using
fastify.view
, or in my case,fastify.render
as I renamed the property using "propertyName" option. I use Handlebars as an engine. I also have a pre-handler set in one of my custom plugins to extendreply.locals
(as described here).The problem is that
fastify.view
renders the template without passingreply.locals
, whereasreply.view
renders the template as expected withreply.locals
.Steps to Reproduce
Application
Layout
Template
Expected Behavior
Rendering with
fastify.view
should work the same way asreply.view
except it returns the rendering result without sending it.The text was updated successfully, but these errors were encountered: