Skip to content

A small wrapper around html/template

Notifications You must be signed in to change notification settings

hostwithquantum/tpl

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

tpl

A small wrapper around html/template. This is not optimized for performance, but optimized for using this library to write somewhat decent code.

Example

Put your files into a templates/ directory.

The only required file is a layout.gotmpl with appropriate blocks for the layout and content.

{{ define "layout" }}
<!DOCTYPE html>
<html lang="en">
<body>
  <main class="container">
    {{ template "content" . }}
  </main>
</body>
</html>
{{ end }}

Initialize:

var (
	//go:embed templates/*
	tmplFS embed.FS

)
renderer := tpl.NewRender(tmplFS, "name")

... and pass it into your router/handlers.

Define a struct like this and assign data to it so your templates have something to show.

type tplData struct {
	Data any
} 

Output a template from your handler:

// filename.gotmpl
tpl.Render(w, "filename", tplData{
	Data: allData,
})

About

A small wrapper around html/template

Resources

Stars

Watchers

Forks

Releases

No releases published

Languages