updated for using .env
This commit is contained in:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user