cachy

package module
v1.0.9 Latest Latest
Warning

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

Go to latest
Published: Feb 10, 2020 License: MIT Imports: 13 Imported by: 0

README

Go Report Card

Cachy - HTML Template Caching Library

Cachy is a simple caching library for templates using Go's html/template package.

Features

  • accepts explicit folders or scans/watches complete directory
  • execute single templates
  • execute multiple templates (as with ParseFiles("file1", "file2"...))
  • filewatcher that updates the cache on template changes
  • reloading browser on template change via JavaScript fetch
  • allow duplicate template files
  • add folders recursively

Usage

If you explicitly set folders, you can tell Cachy to search for nested folders via the "recursive" argument. If no folder is given, Cachy will look for template files in the current folder recursively.

The "allowDuplicates" parameter checks, if templates with the same filename can co-exist or not. If duplications are disallowed, executing templates is easily done by just providing the filename minus the extension. If duplicates are allowed, you have to include the whole path.

Simple Example:
c, _ := cachy.New(nil, nil) // this will process all *.html files, no FuncMap, no duplicates, will search for template files within whole working dir
go c.Watch(true) // starts the filewatcher, logging enabled

_ := c.Execute(w, nil, "template", "template2") // io.Writer, data, templates...
Hot-Reloading browser via JavaScript
...

c, _ := cachy.New(nil, nil)
go c.Watch(false)

http.Handle(c.URL(), c.SSE)

...

In your template you simply have to execute the "reloadScript" template-function.

// end of <body> or wherever you want
{{ reloadScript }}

Benchmarks

BenchmarkDefaultSingle-16        4395246               468 ns/op             807 B/op          2 allocs/op
BenchmarkDefaultMultiple-16      4934824               412 ns/op             743 B/op          2 allocs/op
BenchmarkCachySingle-16          4513342               455 ns/op             789 B/op          2 allocs/op
BenchmarkCachyMultiple-16        3543176               567 ns/op            1013 B/op          3 allocs/op

If you have suggestions or feedback, feel free to contact me! PRs or Issues are welcomed!

I hope this little library is useful to some.

Regards

Andrej Benz

Documentation

Overview

Package cachy is a package that can be used to cache and execute templates.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cachy

type Cachy struct {
	SSE *broker
	// contains filtered or unexported fields
}

Cachy represents the template cache

func New

func New(p *Params, funcs template.FuncMap, folders ...string) (c *Cachy, err error)

New processes all templates and returns a populated Cachy struct. You can provide template folders, otherwise it will scan the whole working dir for templates.

func (*Cachy) Execute

func (c *Cachy) Execute(w io.Writer, data interface{}, files ...string) (err error)

Execute executes the given template(s).

func (*Cachy) GetString

func (c *Cachy) GetString(file string) string

GetString returns the string representation of the given template.

func (Cachy) URL added in v1.0.7

func (c Cachy) URL() string

URL returns the hot-reload URL

func (*Cachy) Watch

func (c *Cachy) Watch(debug bool) error

Watch is used to monitor file changes and update the template cache. You can set debug = true if you want Cachy to output log entries on an event.

type Params added in v1.0.7

type Params struct {
	URL        string
	Ext        string
	Duplicates bool
	Recursive  bool
}

Params can be used to modify Cachy's default behaviour.

Directories

Path Synopsis
example
cmd

Jump to

Keyboard shortcuts

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