tmpls

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Jul 5, 2025 License: MIT Imports: 6 Imported by: 0

README

tmpls

A lightweight, thread-safe tool for working with go html templates.

Usage

package main

import (
    "embed"
    "log/slog"
    "os"
    
    "github.com/fivethirty/tmpls"
)

//go:embed templates
var templatesFS embed.FS

func main() {
    // Embedded templates (production)
    tmpls, err := tmpls.New(
        tmpls.Config{
            TemplatesFS: templatesFS,
            CommonGlob:  "common/*.html.tmpl",
        },
        slog.Default(),
    )
    
    // Filesystem templates (development)
    tmpls, err := tmpls.New(
        tmpls.Config{
            TemplatesFS:  os.DirFS("./templates"),
            DisableCache: true, // Enable hot-swapping
            CommonGlob:   "common/*.html.tmpl",
        },
        slog.Default(),
    )
    
    // Execute a template
    output, err := tmpls.Execute(
        "*.html.tmpl",     // glob pattern to parse
        "page.html.tmpl",  // template name to execute
        data,              // template data
    )
}

Config

  • TemplatesFS - Any fs.FS containing templates
  • DisableCache - Disable caching for hot-swapping (default: false)
  • CommonGlob - Pattern for common templates included in all parses

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	TemplatesFS  fs.FS
	DisableCache bool
	CommonGlob   string
}

type Templates

type Templates struct {
	// contains filtered or unexported fields
}

func New

func New(config Config, logger *slog.Logger) (*Templates, error)

func (*Templates) Execute

func (t *Templates) Execute(
	glob string,
	template string,
	data any,
) (string, error)

Jump to

Keyboard shortcuts

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