Skip to content

Instantly share code, notes, and snippets.

@inhies
Last active December 26, 2015 09:39
Show Gist options
  • Save inhies/7131357 to your computer and use it in GitHub Desktop.
Save inhies/7131357 to your computer and use it in GitHub Desktop.
Parse all templates in the current directory and all sub directories, then executes the given template name. Run as `ttest <template name>`.
package main
import (
"fmt"
"html/template"
"os"
"path/filepath"
"strings"
)
func main() {
t := template.New("template")
err := filepath.Walk("./",
func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() || !strings.HasSuffix(path, ".html") {
return nil // Ignore directories and other files
}
_, err = t.ParseFiles(path) // Parse the template
return err
})
var useTemplate string
if len(os.Args) < 2 {
fmt.Println("No template specified, running 'index' by default")
useTemplate = "index"
} else {
for i := 1; i < len(os.Args); i++ {
useTemplate += os.Args[i]
if i < len(os.Args)-1 {
useTemplate += " "
}
}
}
// Print out the completed template
err = t.ExecuteTemplate(os.Stdout, useTemplate, nil)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment