Compare commits

...

2 Commits

2 changed files with 240 additions and 47 deletions

View File

@ -21,6 +21,8 @@ type (
funcMap template.FuncMap funcMap template.FuncMap
cache map[string]*template.Template cache map[string]*template.Template
cacheMu sync.RWMutex cacheMu sync.RWMutex
baseTmpl *template.Template
baseOnce sync.Once
} }
) )
@ -29,9 +31,12 @@ const (
ContentTextHTMLUTF8 string = "text/html; charset=utf-8" ContentTextHTMLUTF8 string = "text/html; charset=utf-8"
) )
// NewHTMLRender initializes a new Render instance. // NewHTMLRender initializes a new HRender instance, setting up the template engine.
// fileSystem: The file system containing the templates (usually embed.FS or os.DirFS). // It configures the file system to load templates from, enables or disables caching,
// enableCache: If true, templates will be compiled once and cached for future use. // and registers a default "dict" function for template use.
//
// fileSystem: The `fs.FS` implementation from which templates will be loaded (e.g., `embed.FS` or `os.DirFS`).
// enableCache: If `true`, compiled templates will be stored in an in-memory cache for faster retrieval on subsequent renders.
func NewHTMLRender(fileSystem fs.FS, enableCache bool) *HRender { func NewHTMLRender(fileSystem fs.FS, enableCache bool) *HRender {
return &HRender{ return &HRender{
fs: fileSystem, fs: fileSystem,
@ -43,20 +48,83 @@ func NewHTMLRender(fileSystem fs.FS, enableCache bool) *HRender {
} }
} }
// AddFunc registers a custom function to the template's FuncMap. // AddFunc registers a custom function with the template's FuncMap.
// This must be called before rendering any templates if you want the function to be available. // This method must be called before any templates are rendered for the function
// to be available within the templates.
//
// name: The name by which the function will be called within the template.
// fn: The Go function to be registered. Its signature must be compatible with `html/template` expectations.
func (r *HRender) AddFunc(name string, fn any) { func (r *HRender) AddFunc(name string, fn any) {
r.funcMap[name] = fn r.funcMap[name] = fn
} }
// Render executes the template for the given pageName and writes the result to w. // RenderW executes the specified template (pageName) with the provided data and writes the
// pageName: The path to the page template (e.g., "pages/login.html"). // resulting HTML to the `http.ResponseWriter`. It can optionally apply a layout template.
// data: The data map to be passed to the template.
// layoutName: (Optional) The path to the layout template (e.g., "layouts/base.html").
// //
// If caching is enabled, it checks the cache first. If not found (or cache disabled), // This function handles template compilation (with caching if enabled) and execution,
// it compiles the template (merging layout and page) and caches it if appropriate. // ensuring that any errors during execution do not result in partial responses.
func (r *HRender) Render(w http.ResponseWriter, pageName string, data H, layoutName ...string) error { //
// w: The `http.ResponseWriter` to which the rendered HTML will be written.
// pageName: The base name of the page template to render (e.g., "pages/login.html").
// data: A map of data (`H`) to be passed into the template for dynamic content generation.
// layoutName: (Optional) The name of the layout template to wrap the page content (e.g., "layouts/base.html").
//
// The page content will be embedded where `{{ embed }}` or `{{embed}}` is found in the layout.
func (r *HRender) RenderW(w http.ResponseWriter, pageName string, data H, layoutName ...string) error {
tmpl, err := r.getTemplateInstance(pageName, layoutName...)
if err != nil {
return err
}
return r.execute(w, tmpl, data)
}
// RenderS executes the specified template (pageName) with the provided data and
// returns the resulting HTML as a string. It can optionally apply a layout template.
//
// This function is useful for scenarios where the rendered HTML needs to be further processed,
// sent as part of an SSE stream, or returned as a plain string rather than directly written
// to an `http.ResponseWriter`.
//
// pageName: The base name of the page template to render (e.g., "pages/login.html").
// data: A map of data (`H`) to be passed into the template for dynamic content generation.
// layoutName: (Optional) The name of the layout template to wrap the page content (e.g., "layouts/base.html").
//
// The page content will be embedded where `{{ embed }}` or `{{embed}}` is found in the layout.
//
// Returns:
//
// A string containing the rendered HTML.
// An error if template compilation or execution fails.
func (r *HRender) RenderS(pageName string, data H, layoutName ...string) (string, error) {
tmpl, err := r.getTemplateInstance(pageName, layoutName...)
if err != nil {
return "", err
}
var buf bytes.Buffer
if err := tmpl.Execute(&buf, data); err != nil {
return "", err
}
return buf.String(), nil
}
// getTemplateInstance retrieves a compiled template. It first checks if the template
// is available in the cache (if caching is enabled). If not found or caching is disabled,
// it proceeds to build the template by combining the page and optional layout, and
// parsing all associated fragments. If caching is enabled, the newly built template
// is stored in the cache before being returned.
//
// pageName: The name of the main page template.
// layoutName: (Optional) The name of the layout template.
//
// Returns:
//
// A pointer to `template.Template` instance ready for execution.
// An error if template files cannot be read or parsing fails.
func (r *HRender) getTemplateInstance(pageName string, layoutName ...string) (*template.Template, error) {
if !strings.HasSuffix(pageName, ".html") { if !strings.HasSuffix(pageName, ".html") {
pageName += ".html" pageName += ".html"
} }
@ -82,37 +150,40 @@ func (r *HRender) Render(w http.ResponseWriter, pageName string, data H, layoutN
cachedTmpl, ok := r.cache[cacheKey] cachedTmpl, ok := r.cache[cacheKey]
r.cacheMu.RUnlock() r.cacheMu.RUnlock()
if ok { if ok {
return r.execute(w, cachedTmpl, data) return cachedTmpl, nil // Retorna si está en caché
} }
r.cacheMu.Lock() r.cacheMu.Lock()
defer r.cacheMu.Unlock()
if cachedTmpl, ok = r.cache[cacheKey]; ok { if cachedTmpl, ok = r.cache[cacheKey]; ok {
r.cacheMu.Unlock() return cachedTmpl, nil
return r.execute(w, cachedTmpl, data)
} }
tmpl, err = r.buildTemplate(pageName, layout) tmpl, err = r.buildTemplate(pageName, layout)
if err != nil { if err != nil {
r.cacheMu.Unlock() return nil, err
return err
} }
r.cache[cacheKey] = tmpl r.cache[cacheKey] = tmpl
slog.Debug("template compiled and cached", "key", cacheKey) slog.Debug("template compiled and cached", "key", cacheKey)
r.cacheMu.Unlock() return tmpl, nil
} else { } else {
tmpl, err = r.buildTemplate(pageName, layout) return r.buildTemplate(pageName, layout)
if err != nil {
return err
}
} }
return r.execute(w, tmpl, data)
} }
// execute runs the template into a buffer first to catch any execution errors // execute renders the provided template with data into an internal buffer.
// before writing to the response writer. This prevents partial writes on error. // If successful, it writes the buffer's content to the `http.ResponseWriter`.
// This prevents partial HTTP responses in case of template execution errors.
//
// w: The `http.ResponseWriter` to write the rendered content to.
// tmpl: The `*template.Template` instance to execute.
// data: The data map (`H`) to pass to the template.
//
// Returns:
//
// An error if template execution or writing to the response writer fails.
func (r *HRender) execute(w http.ResponseWriter, tmpl *template.Template, data H) error { func (r *HRender) execute(w http.ResponseWriter, tmpl *template.Template, data H) error {
var buf bytes.Buffer var buf bytes.Buffer
if err := tmpl.Execute(&buf, data); err != nil { if err := tmpl.Execute(&buf, data); err != nil {
@ -122,11 +193,23 @@ func (r *HRender) execute(w http.ResponseWriter, tmpl *template.Template, data H
return err return err
} }
// buildTemplate constructs a new template instance by: // buildTemplate constructs a new template instance by cloning a pre-loaded base template
// 1. Reading the page content. // (containing shared components/fragments) and parsing the specific page and layout.
// 2. Reading the layout content (if provided) and embedding the page content into it using {{ embed }}. // It ensures shared templates are loaded only once.
// 3. Walking the file system to parse all other available templates (fragments/components) so they can be invoked.
func (r *HRender) buildTemplate(pageName, layoutName string) (*template.Template, error) { func (r *HRender) buildTemplate(pageName, layoutName string) (*template.Template, error) {
var initErr error
r.baseOnce.Do(func() {
initErr = r.loadSharedTemplates()
})
if initErr != nil {
return nil, fmt.Errorf("failed to load shared templates: %w", initErr)
}
tmpl, err := r.baseTmpl.Clone()
if err != nil {
return nil, fmt.Errorf("failed to clone base template: %w", err)
}
pageContent, err := fs.ReadFile(r.fs, pageName) pageContent, err := fs.ReadFile(r.fs, pageName)
if err != nil { if err != nil {
return nil, fmt.Errorf("page not found: %w", err) return nil, fmt.Errorf("page not found: %w", err)
@ -141,7 +224,6 @@ func (r *HRender) buildTemplate(pageName, layoutName string) (*template.Template
} }
layoutStr := string(layoutBytes) layoutStr := string(layoutBytes)
layoutStr = strings.ReplaceAll(layoutStr, "{{ embed }}", string(pageContent)) layoutStr = strings.ReplaceAll(layoutStr, "{{ embed }}", string(pageContent))
layoutStr = strings.ReplaceAll(layoutStr, "{{embed}}", string(pageContent)) layoutStr = strings.ReplaceAll(layoutStr, "{{embed}}", string(pageContent))
@ -150,13 +232,19 @@ func (r *HRender) buildTemplate(pageName, layoutName string) (*template.Template
finalContent = string(pageContent) finalContent = string(pageContent)
} }
tmpl := template.New("root").Funcs(r.funcMap)
if _, err := tmpl.Parse(finalContent); err != nil { if _, err := tmpl.Parse(finalContent); err != nil {
return nil, fmt.Errorf("error parsing main content: %w", err) return nil, fmt.Errorf("error parsing main content: %w", err)
} }
err = fs.WalkDir(r.fs, ".", func(path string, d fs.DirEntry, err error) error { return tmpl, nil
}
// loadSharedTemplates scans the file system for templates in "components/" and "fragments/"
// directories and parses them into a base template instance.
func (r *HRender) loadSharedTemplates() error {
r.baseTmpl = template.New("root").Funcs(r.funcMap)
err := fs.WalkDir(r.fs, ".", func(path string, d fs.DirEntry, err error) error {
if err != nil { if err != nil {
return err return err
} }
@ -167,7 +255,8 @@ func (r *HRender) buildTemplate(pageName, layoutName string) (*template.Template
return nil return nil
} }
if path == pageName || path == layoutName { pathSlash := filepath.ToSlash(path)
if !strings.HasPrefix(pathSlash, "components/") && !strings.HasPrefix(pathSlash, "fragments/") {
return nil return nil
} }
@ -176,17 +265,17 @@ func (r *HRender) buildTemplate(pageName, layoutName string) (*template.Template
return err return err
} }
name := filepath.ToSlash(path) name := pathSlash
name = strings.TrimSuffix(name, filepath.Ext(name)) name = strings.TrimSuffix(name, filepath.Ext(name))
_, err = tmpl.New(name).Parse(string(content)) _, err = r.baseTmpl.New(name).Parse(string(content))
return err return err
}) })
if err != nil { if err != nil {
return nil, err return fmt.Errorf("error loading shared templates: %w", err)
} }
return tmpl, nil return nil
} }
// Dict creates a map[string]any from a list of key-value pairs. // Dict creates a map[string]any from a list of key-value pairs.

View File

@ -11,7 +11,7 @@ import (
"testing/fstest" "testing/fstest"
) )
func Test_Render(t *testing.T) { func Test_RenderW(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
mockFS fstest.MapFS mockFS fstest.MapFS
@ -90,13 +90,37 @@ func Test_Render(t *testing.T) {
expectedBody: `<button id="myButton" class="btn btn-primary">Click Me</button>`, expectedBody: `<button id="myButton" class="btn btn-primary">Click Me</button>`,
}, },
{ {
name: "error page not found due to wrong extension", name: "auto-append .html to page",
mockFS: fstest.MapFS{ mockFS: fstest.MapFS{
"index.another": { "about.html": {
Data: []byte(`<h1>Hello, wrong extension!</h1>`), Data: []byte(`<h1>About</h1>`),
}, },
}, },
page: "index.another", page: "about",
expectedBody: "<h1>About</h1>",
},
{
name: "auto-append .html to layout",
mockFS: fstest.MapFS{
"contact.html": {
Data: []byte(`<p>Contact</p>`),
},
"base.html": {
Data: []byte(`<div>{{ embed }}</div>`),
},
},
page: "contact.html",
layout: "base",
expectedBody: "<div><p>Contact</p></div>",
},
{
name: "error page not found",
mockFS: fstest.MapFS{
"index.html": {
Data: []byte(`<h1>Exists</h1>`),
},
},
page: "missing.html",
expectedError: true, expectedError: true,
expectedBody: "", expectedBody: "",
}, },
@ -117,7 +141,7 @@ func Test_Render(t *testing.T) {
layouts = append(layouts, tt.layout) layouts = append(layouts, tt.layout)
} }
err := r.Render(rec, tt.page, tt.data, layouts...) err := r.RenderW(rec, tt.page, tt.data, layouts...)
if tt.expectedError { if tt.expectedError {
if err == nil { if err == nil {
t.Error("expected error, got nil") t.Error("expected error, got nil")
@ -134,6 +158,83 @@ func Test_Render(t *testing.T) {
} }
} }
func Test_RenderS(t *testing.T) {
mockFS := fstest.MapFS{
"index.html": {
Data: []byte(`<h1>{{ .Title }}</h1>`),
},
}
r := NewHTMLRender(mockFS, false)
data := H{"Title": "Render String"}
got, err := r.RenderS("index.html", data)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
expected := "<h1>Render String</h1>"
if got != expected {
t.Errorf("expected %q, got %q", expected, got)
}
}
func Test_SharedTemplatesIsolation(t *testing.T) {
// This test ensures that ONLY components/ and fragments/ are loaded into the base template
mockFS := fstest.MapFS{
"pages/home.html": {
Data: []byte(`
{{ template "components/btn" . }}
{{ template "fragments/tbl" . }}
{{ template "other/ignored" . }}
`),
},
"components/btn.html": {
Data: []byte(`[BUTTON]`),
},
"fragments/tbl.html": {
Data: []byte(`[TABLE]`),
},
"other/ignored.html": {
Data: []byte(`[IGNORED]`),
},
}
r := NewHTMLRender(mockFS, false)
// Attempt to render home. It should fail because "other/ignored" is not available in the base template,
// and "pages/home.html" only tries to invoke it, it doesn't define it.
err := r.RenderW(httptest.NewRecorder(), "pages/home.html", nil)
if err == nil {
t.Fatal("expected error due to missing 'other/ignored' template, but got nil")
}
// Now a valid test case checking correct loading of components and fragments
mockFSValid := fstest.MapFS{
"pages/valid.html": {
Data: []byte(`{{ template "components/btn" . }} - {{ template "fragments/tbl" . }}`),
},
"components/btn.html": {
Data: []byte(`[BUTTON]`),
},
"fragments/tbl.html": {
Data: []byte(`[TABLE]`),
},
}
r2 := NewHTMLRender(mockFSValid, false)
rec := httptest.NewRecorder()
err = r2.RenderW(rec, "pages/valid.html", nil)
if err != nil {
t.Fatalf("unexpected error rendering valid templates: %v", err)
}
expected := "[BUTTON] - [TABLE]"
if got := rec.Body.String(); got != expected {
t.Errorf("expected %q, got %q", expected, got)
}
}
func Test_Dict(t *testing.T) { func Test_Dict(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
@ -198,7 +299,7 @@ func Test_Cache(t *testing.T) {
r := NewHTMLRender(mockFS, true) r := NewHTMLRender(mockFS, true)
rec1 := httptest.NewRecorder() rec1 := httptest.NewRecorder()
err1 := r.Render(rec1, "index.html", nil, "main-layout.html") err1 := r.RenderW(rec1, "index.html", nil, "main-layout.html")
if err1 != nil { if err1 != nil {
t.Errorf("unexpected error: %v", err1) t.Errorf("unexpected error: %v", err1)
} }
@ -207,6 +308,7 @@ func Test_Cache(t *testing.T) {
t.Errorf("expected body %q, got %q", expected, got) t.Errorf("expected body %q, got %q", expected, got)
} }
// Verify log contains "template compiled and cached"
if !strings.Contains(buf.String(), "template compiled and cached") { if !strings.Contains(buf.String(), "template compiled and cached") {
t.Error("expected 'template compiled and cached' message on first render") t.Error("expected 'template compiled and cached' message on first render")
} }
@ -214,7 +316,7 @@ func Test_Cache(t *testing.T) {
buf.Reset() buf.Reset()
rec2 := httptest.NewRecorder() rec2 := httptest.NewRecorder()
err2 := r.Render(rec2, "index.html", nil, "main-layout.html") err2 := r.RenderW(rec2, "index.html", nil, "main-layout.html")
if err2 != nil { if err2 != nil {
t.Errorf("unexpected error: %v", err2) t.Errorf("unexpected error: %v", err2)
} }
@ -222,7 +324,9 @@ func Test_Cache(t *testing.T) {
t.Errorf("expected body %q, got %q", expected, got) t.Errorf("expected body %q, got %q", expected, got)
} }
// Verify log DOES NOT contain "template compiled and cached" (cache hit)
if strings.Contains(buf.String(), "template compiled and cached") { if strings.Contains(buf.String(), "template compiled and cached") {
t.Error("did not expect 'template compiled and cached' message on second render") t.Error("did not expect 'template compiled and cached' message on second render")
} }
} }