Project

General

Profile

Actions

Email Notification Service

package test

import (
	"bytes"
	"fmt"
	"html/template"
	"path/filepath"
)

// RenderHTML renders notification.html with the provided data.
func (d NotificationTemplateData) RenderHTML(templatePath string) (string, error) {
	if templatePath == "" {
		return "", fmt.Errorf("template path is required")
	}

	tmpl, err := template.ParseFiles(templatePath)
	if err != nil {
		return "", fmt.Errorf("parse template: %w", err)
	}

	var buf bytes.Buffer
	templateName := filepath.Base(templatePath)
	if err := tmpl.ExecuteTemplate(&buf, templateName, d); err != nil {
		return "", fmt.Errorf("render template: %w", err)
	}

	return buf.String(), nil
}

Updated by karnake ruengchaicharnkij 9 days ago · 1 revisions