diff options
Diffstat (limited to 'lib/mail/mailer.ts')
| -rw-r--r-- | lib/mail/mailer.ts | 39 |
1 files changed, 35 insertions, 4 deletions
diff --git a/lib/mail/mailer.ts b/lib/mail/mailer.ts index 200a0ed9..3474a373 100644 --- a/lib/mail/mailer.ts +++ b/lib/mail/mailer.ts @@ -15,14 +15,45 @@ const transporter = nodemailer.createTransport({ }, }); -// Handlebars 템플릿 로더 함수 -function loadTemplate(templateName: string, data: Record<string, any>) { +// // Handlebars 템플릿 로더 함수 +// function loadTemplate(templateName: string, data: Record<string, any>) { +// const templatePath = path.join(process.cwd(), 'lib', 'mail', 'templates', `${templateName}.hbs`); +// const source = fs.readFileSync(templatePath, 'utf8'); +// const template = handlebars.compile(source); +// return template(data); +// } +function applyLayout(layoutName: string, content: string, context: Record<string, any>) { + const layoutPath = path.join(process.cwd(), 'lib', 'mail', 'layouts', `${layoutName}.hbs`); + const layoutSource = fs.readFileSync(layoutPath, 'utf8'); + const layoutTemplate = handlebars.compile(layoutSource); + return layoutTemplate({ ...context, body: content }); +} + +// Partials 자동 등록 +function registerPartials() { + const partialsDir = path.join(process.cwd(), 'lib', 'mail', 'partials'); + const filenames = fs.readdirSync(partialsDir); + + filenames.forEach((filename) => { + const name = path.parse(filename).name; + const filepath = path.join(partialsDir, filename); + const source = fs.readFileSync(filepath, 'utf8'); + handlebars.registerPartial(name, source); // {{> header }}, {{> footer }} + }); +} + + +// 템플릿 불러오기 + layout/partials 적용 +function loadTemplate(templateName: string, context: Record<string, any>, layout = 'base') { + registerPartials(); + const templatePath = path.join(process.cwd(), 'lib', 'mail', 'templates', `${templateName}.hbs`); const source = fs.readFileSync(templatePath, 'utf8'); const template = handlebars.compile(source); - return template(data); -} + const content = template(context); // 본문 먼저 처리 + return applyLayout(layout, content, context); // base.hbs로 감싸기 +} handlebars.registerHelper('t', function(key: string, options: any) { // options.hash에는 Handlebars에서 넘긴 named parameter들(location=location 등)이 들어있음 return i18next.t(key, options.hash || {}); |
