updated for using .env

This commit is contained in:
Mann Patel
2025-08-27 17:54:45 -06:00
parent 09093aa70a
commit 9f260c3821
11 changed files with 83 additions and 32 deletions

View File

@@ -2,9 +2,14 @@ package utils
import (
"bytes"
"fmt"
"html/template"
"log"
"net/http"
"os"
"path/filepath"
"github.com/joho/godotenv"
)
// Helper functions for templates
@@ -44,9 +49,19 @@ var templateFuncs = template.FuncMap{
}
func Render(w http.ResponseWriter, tmpl string, data interface{}) {
// Load .env file
err := godotenv.Load() // or specify path: godotenv.Load("/path/to/.env")
if err != nil {
log.Fatalf("Error loading .env file: %v", err)
}
// Get individual components from environment variables
TemplatesURL := os.Getenv("TEMPLATES_URL")
fmt.Print(TemplatesURL)
// Paths for layout + page templates
layout := filepath.Join("/Users/mannpatel/Desktop/Poll-system/app/internal/templates/", "layout.html")
page := filepath.Join("/Users/mannpatel/Desktop/Poll-system/app/internal/templates/", tmpl)
layout := filepath.Join(TemplatesURL, "layout.html")
page := filepath.Join(TemplatesURL, tmpl)
// Parse files with helper functions
tmpls, err := template.New("").Funcs(templateFuncs).ParseFiles(layout, page)