tmplreload

package module
v0.0.0-...-6952b2b Latest Latest
Warning

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

Go to latest
Published: Feb 16, 2024 License: AGPL-3.0 Imports: 8 Imported by: 0

README

tmplreload - Auto-Reloading Templates for Go

tmplreload is a Go module that provides auto-reloading templates based on the html/template package. It allows you to create templates that automatically reload when the underlying template file changes. This can be particularly useful during development when you want to see the changes to your templates without restarting your application.

Features

  • Auto-Reloading: Templates are automatically reloaded when the underlying file changes.
  • Function Map Management: Easily add, remove, or update template functions.

Installation

To use tmplreload in your Go project, you can simply run:

go get -u github.com/NIR3X/tmplreload

Example Usage

package main

import (
	"bytes"
	"fmt"
	"html/template"
	"net/http"
	"time"

	"github.com/NIR3X/tmplreload"
)

func main() {
	// Create a new TmplColl (Template Collection).
	tmplColl := tmplreload.NewTmplColl(60, 1)

	// Close the TmplColl when the main function ends.
	defer tmplColl.Close()

	// Define a function to be used in the template.
	funcMap := template.FuncMap{
		"currentTime": func() string {
			return time.Now().Format(time.RFC3339)
		},
	}

	// Add the function to the TmplColl function map.
	tmplColl.FuncsAdd(funcMap)

	// Parse template files in the "templates" directory.
	err := tmplColl.ParseGlob("templates/*.html")
	if err != nil {
		fmt.Println("Error parsing templates:", err)
		return
	}

	// Start an HTTP server to render the templates.
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		// Execute the template and pass data.
		data := struct{ Message string }{"Hello, tmplreload!"}
		err := tmplColl.ExecuteTemplate(w, "templates/index.html", data)
		if err != nil {
			http.Error(w, "Error rendering template", http.StatusInternalServerError)
		}
	})

	// Start the server on port 8000.
	fmt.Println("Server is running on http://127.0.0.1:8000")
	http.ListenAndServe(":8000", nil)
}

Example HTML Template (templates/index.html)

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Example Template</title>
</head>
<body>
	<h1>{{ .Message }}!</h1>
	<p>Current time: {{ currentTime }}</p>
</body>
</html>

In this example, the tmplreload module is used to create a template collection (TmplColl). Templates are parsed from the "templates" directory, and a function (currentTime) is added to the function map. The HTTP server renders the template on incoming requests.

Documentation

License

GNU AGPLv3 Image

This program is Free Software: You can use, study share and improve it at your will. Specifically you can redistribute and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CollTmpl

type CollTmpl interface {
	Delims(left, right string)
	FuncAdd(name string, function interface{})
	FuncsAdd(funcMap template.FuncMap)
	FuncsRemove(names ...string)
	Option(opt ...string)
	Reload() error
	Execute(wr io.Writer, data interface{}) (err error)
}

A collection template.

type Tmpl

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

A template that reloads itself when the underlying file changes.

func NewTmpl

func NewTmpl(minUpdateIntvlSecs ...int64) *Tmpl

Creates a new template.

func (*Tmpl) Delims

func (t *Tmpl) Delims(left, right string)

Sets the delimiters to the specified strings.

func (*Tmpl) Execute

func (t *Tmpl) Execute(wr io.Writer, data interface{}) (err error)

Executes the template.

func (*Tmpl) FuncAdd

func (t *Tmpl) FuncAdd(name string, function interface{})

Adds the template function to the function map.

func (*Tmpl) FuncsAdd

func (t *Tmpl) FuncsAdd(funcMap template.FuncMap)

Adds the template functions to the function map.

func (*Tmpl) FuncsRemove

func (t *Tmpl) FuncsRemove(names ...string)

Removes the template functions from the function map.

func (*Tmpl) Option

func (t *Tmpl) Option(opt ...string)

Sets options for the template.

func (*Tmpl) ParseFile

func (t *Tmpl) ParseFile(filename string) error

Parses the named file.

func (*Tmpl) Reload

func (t *Tmpl) Reload() error

Reloads the template immediately.

type TmplColl

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

A struct that manages a collection of templates.

func NewTmplColl

func NewTmplColl(cleanupIntvlSecs, minUpdateIntvlSecs int64) *TmplColl

Creates a new TmplColl. e. g. NewTmplColl(60, 1)

func (*TmplColl) Close

func (t *TmplColl) Close()

Stops the TmplColl from removing stale templates.

func (*TmplColl) Delims

func (t *TmplColl) Delims(left, right string)

Sets default delimiters for new templates.

func (*TmplColl) ExecuteTemplate

func (t *TmplColl) ExecuteTemplate(wr io.Writer, filename string, data interface{}) error

Executes the template associated with the given filename.

func (*TmplColl) FuncAdd

func (t *TmplColl) FuncAdd(name string, function interface{})

Adds the template function to the function map.

func (*TmplColl) FuncsAdd

func (t *TmplColl) FuncsAdd(funcMap template.FuncMap)

Adds the template functions to the function map.

func (*TmplColl) FuncsRemove

func (t *TmplColl) FuncsRemove(names ...string)

Removes the template functions from the function map.

func (*TmplColl) Lookup

func (t *TmplColl) Lookup(filename string) CollTmpl

Returns the template associated with the given filename.

func (*TmplColl) Option

func (t *TmplColl) Option(opt ...string)

Sets options for new templates.

func (*TmplColl) ParseFiles

func (t *TmplColl) ParseFiles(filenames ...string) error

Parses the files and associates the resulting templates with filenames.

func (*TmplColl) ParseGlob

func (t *TmplColl) ParseGlob(pattern string) error

Parses the files and associates the resulting templates with filenames.

func (*TmplColl) ReloadFiles

func (t *TmplColl) ReloadFiles(filenames ...string) error

Reloads the templates associated with the given filenames.

func (*TmplColl) RemoveFiles

func (t *TmplColl) RemoveFiles(filenames ...string)

Removes the templates associated with the given filenames.

func (*TmplColl) RemoveStaleFiles

func (t *TmplColl) RemoveStaleFiles()

Removes templates that no longer exist.

Jump to

Keyboard shortcuts

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