Last active
December 26, 2015 09:39
-
-
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>`.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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