示例#1
0
文件: gen.go 项目: Bobberino/musings
func main() {
	t := template.New(nil)
	t.SetDelims("«", "»")
	err := t.Parse(templateStr)
	if err != nil {
		log.Fatal(err)
	}
	err = t.Execute(os.Stdout, data)
	if err != nil {
		log.Fatal(err)
	}
}
示例#2
0
文件: handlers.go 项目: awong-dev/lwb
func ReloadTemplates(config *lwb.BlogConfig) {
	fileInfos, err := ioutil.ReadDir(*flagTemplatePath)
	if err != nil {
		panic("Failed to scan for templates: " + err.String())
	}

	for _, fileInfo := range fileInfos {
		if !strings.HasSuffix(fileInfo.Name, templateSuffix) {
			continue
		}
		basename := fileInfo.Name[:len(fileInfo.Name)-len(templateSuffix)]
		_, present := templates[basename]
		if !present || templates[basename].Timestamp < fileInfo.Mtime_ns {
			path := path.Join(*flagTemplatePath, fileInfo.Name)
			tmpl := template.New(
				template.FormatterMap{
					"textile":          textile.TextileFormatter,
					"textileFullLinks": textile.GetTextileFullLinkFormatter(config.BlogUrl.String()),
					"entities":         textile.EncodeEntitiesFormatter,
					"spaces":           lwb.EncodeSpacesFormatter,
					"convertbreaks":    lwb.ConvertBreaksFormatter,
				})
			tmpl.SetDelims("{{", "}}")

			b, err := ioutil.ReadFile(path)
			if err != nil {
				panic("failed to read " + path + ": " + err.String())
			}
			err = tmpl.Parse(string(b))
			if err != nil {
				panic("failed to parse " + path + ": " + err.String())
			}

			templates[basename] = &templateEntry{
				Path:      path,
				Timestamp: fileInfo.Mtime_ns,
				Template:  tmpl,
			}
		}
	}
}