🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more

github.com/joshburnsxyz/go-view-layouts

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/joshburnsxyz/go-view-layouts

v0.0.0-20240601063502-025859b94ef2
100

Supply Chain Security

100

Vulnerability

100

Quality

100

Maintenance

100

License

Network access

Supply chain risk

This module accesses the network.

Found 1 instance in 1 package

Version published
Created

go-view-layouts

This Go library provides a thin wrapper around Go's html/template package for managing and rendering HTML templates with layouts. It ensures thread-safe access to templates, allowing concurrent use in a web server environment.

Features

  • Initialize templates with a common layout
  • Render templates with thread-safe access
  • Handle template parsing and rendering errors gracefully

Installation

go get github.com/joshburnsxyz/go-view-layouts

Usage

Initialization

First, initialize the templates by providing a map of template names to file paths and the layout file path. This should be done once, typically at the start of your application.

package main

import (
	"log"
	"net/http"
	layouts "github.com/joshburnsxyz/go-view-layouts"
)

func main() {
	templateFiles := map[string]string{
		"home":    "templates/home.html",
		"about":   "templates/about.html",
		"contact": "templates/contact.html",
	}

	err := layouts.Init(templateFiles, "templates/layout.html")
	if err != nil {
		log.Fatalf("Failed to initialize templates: %v", err)
	}

	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		layouts.RenderTemplate(w, "home", "layout", nil)
	})

	log.Fatal(http.ListenAndServe(":8080", nil))
}

Rendering Templates

To render a template, call the RenderTemplate function with the response writer, template name, layout name, and data to be injected into the template.

package main

import (
	"net/http"
	layouts "github.com/joshburnsxyz/go-view-layouts"
)

func homeHandler(w http.ResponseWriter, r *http.Request) {
	data := struct {
		Title string
	}{
		Title: "Welcome Home",
	}
	layouts.RenderTemplate(w, "home", "layout", data)
}

func main() {
	// Initialize templates
	templateFiles := map[string]string{
		"home":    "templates/home.html",
		"about":   "templates/about.html",
		"contact": "templates/contact.html",
	}

	err := layouts.Init(templateFiles, "templates/layout.html")
	if err != nil {
		log.Fatalf("Failed to initialize templates: %v", err)
	}

	// Setup handlers
	http.HandleFunc("/", homeHandler)
	http.HandleFunc("/about", func(w http.ResponseWriter, r *http.Request) {
		layouts.RenderTemplate(w, "about", "layout", nil)
	})
	http.HandleFunc("/contact", func(w http.ResponseWriter, r *http.Request) {
		layouts.RenderTemplate(w, "contact", "layout", nil)
	})

	// Start server
	log.Fatal(http.ListenAndServe(":8080", nil))
}

Contributing

If you find a bug or have a feature request, please open an issue on GitHub. Pull requests are welcome!

FAQs

Package last updated on 01 Jun 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts