/dirtmpl

Go directory based template

Primary LanguageGoMIT LicenseMIT

Go Directory Template

Generate go template map based on the directory.

The conventions are:

  • Base template file at _base/__base.*
  • Components template file at _private/* (will not inherit)

Use go standard template, not additional libraries required.

Features

  • Support nested template
  • Support OS file system (relative path)
  • Support go fsys (go embed)
  • Support html/template
  • Support text/template
  • Custom base template folder
  • Custom components folder
  • Exclude specific file
  • Filter by file extension

Usages

Get entries using dirtmpl.Entries() or dirtmpl.EntriesFS()

entries, err := dirtmpl.Entries("samples/simpletxt")
if err != nil {
    log.Fatal(err)
}

for _, entry := range entries {
    fmt.Println(entry.Key, entry.Filenames)
}

Get templates using dirtmpl.TextTemplates(), dirtmpl.TextTemplatesFS(), dirtmpl.HTMLTemplates(), or dirtmpl.HTMLTemplatesFS()

m, err := dirtmpl.TextTemplates("samples/simpletxt")
if err != nil {
    log.Fatal(err)
}

tmpl, ok := m["section_b/subsection_b1.md"]
if !ok {
	log.Fatal("template not found")
}

err = tmpl.Execute(os.Stdout, nil)
if err != nil {
    log.Fatal(err)
}

Examples

  • Generate static html file
  • Web server with go http standard
  • Web server with echo
  • Web server with gofiber

Author

iman.tung@gmail.com