templ

package
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 7, 2025 License: BSD-3-Clause Imports: 6 Imported by: 0

README

HTTP Template Package

The HTTP Template package provides a flexible, hot-reloadable HTML template system for Go web applications with support for layout composition and embedded files.

Features

  • Hot-Reload: Automatic template reloading during development
  • Layout Composition: Support for base layouts and template extension
  • Embedded Files: Compatible with Go 1.16+ embed.FS
  • Template Functions: Custom function maps for template rendering
  • Caching: Production-ready template caching
  • Error Handling: Robust error handling for template issues
  • Cross-Package Integration: Works with handler, server, and logging middleware

Quick Start

package main

import (
    "net/http"
    "os"
    "github.com/alextanhongpin/core/http/templ"
)

func main() {
    tpl := &templ.Template{
        FS:        os.DirFS("templates"),
        HotReload: true, // Set to false in production
    }
    homePage := tpl.Compile("base.html", "pages/home.html")
    aboutPage := tpl.Compile("base.html", "pages/about.html")
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        data := map[string]any{
            "Title": "Home Page",
            "User":  "John Doe",
        }
        homePage.Execute(w, data)
    })
    http.HandleFunc("/about", func(w http.ResponseWriter, r *http.Request) {
        data := map[string]any{
            "Title": "About Us",
        }
        aboutPage.Execute(w, data)
    })
    http.ListenAndServe(":8080", nil)
}

API Reference

Template Initialization
Template{FS: fs.FS, HotReload: bool}

Creates a new template instance.

Compile
Compile(layout string, files ...string) *CompiledTemplate

Compiles templates with optional layout.

Execute
Execute(w http.ResponseWriter, data any) error

Renders template to response writer.

Best Practices

  • Use hot-reload in development, caching in production.
  • Organize templates with layouts and partials for maintainability.
  • Integrate with handler for structured error handling.

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Template

type Template struct {

	// FS is the filesystem to load templates from (e.g. os.DirFS(".") or embed.FS)
	FS fs.FS

	// Funcs provides custom functions available in templates
	Funcs template.FuncMap

	// HotReload reloads templates on each render (for development)
	// Only works with os.DirFS, not embed.FS.
	HotReload bool
	// contains filtered or unexported fields
}

func (*Template) Compile

func (t *Template) Compile(patterns ...string) *Template

func (*Template) Execute

func (t *Template) Execute(wr io.Writer, data any) error

func (*Template) ExecuteTemplate

func (t *Template) ExecuteTemplate(wr io.Writer, name string, data any) error

func (*Template) Extend

func (t *Template) Extend(patterns ...string) *Template

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL