Skip to content

It's not a bug #1

Open
Open
@clabnet

Description

@clabnet

Hi, I need to format datetime into an EJS template, using @fastify/view v9.0.0.
I think to create an helper with my function and pass it on the fastify.view command
const htmlContent = await fastify.view(template, { rfq } )
I have add an hook as is :

declare module 'fastify' {
  interface FastifyReply {
    locals: {
      helpers: any
    }
  }
}
 fastify.addHook('preHandler', (request, reply, done) => {
    // Global variables for the EJS templates can be set here
    reply.locals = {
      helpers: ejsHelpers
    }
    done()
  })

where ejsHelpers contains my custom "formatLocaleDateTime" function.
So, how to pass the helper so that its functions are usable during the rendering phase of the EJS template?
My goal is to use : <%= helpers.formatLocaleDateTime (date()) %>
The error is helpers is not defined.

With best compliments for your work. Thank you.

Activity

eiskalteschatten

eiskalteschatten commented on May 3, 2024

@eiskalteschatten
Owner

The way I did it here: https://github.com/eiskalteschatten/typescript-static-blog/blob/main/src/app.ts works for me and I do use it in my ejs files (for example, here: https://github.com/eiskalteschatten/typescript-static-blog/blob/main/templates/_blog/post.ejs). How are you importing your helpers?

If you have a loose collection of functions (like in https://github.com/eiskalteschatten/typescript-static-blog/blob/main/src/lib/ejsHelpers.ts), then you will need to import it with a * like this: import * as ejsHelpers from './lib/ejsHelpers';

clabnet

clabnet commented on May 5, 2024

@clabnet
Author

@eiskalteschatten I have a plugin with this preHandler hook:

import * as ejsHelpers from '../composables/ejsHelpers'    <<< import here
declare module 'fastify' {
  interface FastifyReply { locals: { helpers: any }  }
}
fastify.addHook('preHandler', (request: RequestCompose, reply: FastifyReply, done: DoneFuncWithErrOrRes) => {
    reply.locals = { helpers: ejsHelpers  }      <<< add helpers to reply
    done()
  })

The controller is :

export default fp(async (fastify: FastifyInstance, opts: FastifyPluginOptions) => {
  fastify.post('/mail/compose', { schema: composeSchema }, async function (request: RequestCompose, reply) {
... validate and get qs and body from the request ...
... get template name ...
... preparing rfqItems data ...
      fastify.log.debug({ rfq, rfqItems }, 'Preparing mail body...')
      const htmlContent = await fastify.view(template, { rfqItems })   <<< merge template with data
      fastify.log.debug({ html: htmlContent }, 'Data merged to mail body')
... send reply 200 ...      

I missed any thing ? Thank's for your reply.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @eiskalteschatten@clabnet

        Issue actions

          It's not a bug · Issue #1 · eiskalteschatten/typescript-static-blog