GithubHelp home page GithubHelp logo

nir3x / tmplreload Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 51 KB

tmplreload - Auto-Reloading Templates for Go

License: GNU Affero General Public License v3.0

Go 100.00%
auto-reload go golang html templates web-development tmplreload

tmplreload's Introduction

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.

tmplreload's People

Contributors

nir3x avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.