Exemple #1
0
func (s *Engine) buildFromAsset() error {
	var templateErr error
	dir := s.Config.Directory
	s.Templates = template.New(dir)
	s.Templates.Delims(s.Config.HTMLTemplate.Left, s.Config.HTMLTemplate.Right)

	for _, path := range s.Config.AssetNames() {
		if !strings.HasPrefix(path, dir) {
			continue
		}

		rel, err := filepath.Rel(dir, path)
		if err != nil {
			panic(err)
		}

		ext := ""
		if strings.Index(rel, ".") != -1 {
			ext = "." + strings.Join(strings.Split(rel, ".")[1:], ".")
		}

		for _, extension := range s.Config.Extensions {
			if ext == extension {

				buf, err := s.Config.Asset(path)
				if err != nil {
					panic(err)
				}
				contents := string(buf)
				name := filepath.ToSlash(rel)
				tmpl := s.Templates.New(name)

				if s.Config.Engine == config.JadeEngine {
					contents, err = jade.Parse(name, contents)
				}
				if err != nil {
					return err
				}

				// Add our funcmaps.
				if s.Config.HTMLTemplate.Funcs != nil {
					tmpl.Funcs(s.Config.HTMLTemplate.Funcs)
				}

				tmpl.Funcs(emptyFuncs).Parse(contents)
				break
			}
		}
	}
	return templateErr
}
Exemple #2
0
func main() {
	dat, err := ioutil.ReadFile("template.jade")
	if err != nil {
		fmt.Printf("ReadFile error: %v", err)
		return
	}

	tmpl, err := jade.Parse("name_of_tpl", string(dat))
	if err != nil {
		fmt.Printf("Parse error: %v", err)
		return
	}

	fmt.Printf("\nOutput:\n\n")
	fmt.Printf("%s", tmpl)
}
Exemple #3
0
func handler(w http.ResponseWriter, r *http.Request) {

	dat, err := ioutil.ReadFile("template.jade")
	if err != nil {
		fmt.Printf("\nReadFile error: %v", err)
		return
	}
	tmpl, err := jade.Parse("jade_tpl", string(dat))
	if err != nil {
		fmt.Printf("\nParse error: %v", err)
		return
	}

	fmt.Printf("%s", tmpl)

	job1 := Job{Employer: "Monash", Role: "Honorary"}
	job2 := Job{Employer: "Box Hill", Role: "Head of HE"}

	person := Person{
		Name:   "jan",
		Age:    50,
		Emails: []string{"*****@*****.**", "*****@*****.**"},
		Jobs:   []*Job{&job1, &job2},
	}

	t, err := template.New("html").Parse(tmpl.String())
	if err != nil {
		fmt.Printf("\nTemplate parse error: %v", err)
		return
	}

	err = t.Execute(w, person)
	if err != nil {
		fmt.Printf("\nExecute error: %v", err)
		return
	}
}
Exemple #4
0
func (s *Engine) buildFromDir() error {
	if s.Config.Directory == "" {
		return nil //we don't return fill error here(yet)
	}

	var templateErr error
	/*var minifier *minify.M
	if s.Config.Minify {
		minifier = minify.New()
		minifier.AddFunc("text/html", htmlMinifier.Minify)
	} // Note: minifier has bugs, I complety remove this from Iris.
	*/
	dir := s.Config.Directory
	s.Templates = template.New(dir)
	s.Templates.Delims(s.Config.HTMLTemplate.Left, s.Config.HTMLTemplate.Right)
	// Walk the supplied directory and compile any files that match our extension list.
	filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
		if info == nil || info.IsDir() {
			return nil
		}

		rel, err := filepath.Rel(dir, path)
		if err != nil {
			return err
		}

		ext := ""
		if strings.Index(rel, ".") != -1 {
			ext = filepath.Ext(rel)
		}

		for _, extension := range s.Config.Extensions {
			if ext == extension {

				buf, err := ioutil.ReadFile(path)
				contents := string(buf)
				/*if s.Config.Minify {
					buf, err = minifier.Bytes("text/html", buf)
				}*/

				if err != nil {
					templateErr = err
					break
				}

				name := filepath.ToSlash(rel)
				tmpl := s.Templates.New(name)

				if s.Config.Engine == config.JadeEngine {
					contents, err = jade.Parse(name, contents)
				}

				if err != nil {
					templateErr = err
					break
				}

				// Add our funcmaps.
				if s.Config.HTMLTemplate.Funcs != nil {
					tmpl.Funcs(s.Config.HTMLTemplate.Funcs)
				}

				tmpl.Funcs(emptyFuncs).Parse(contents)
				break
			}
		}
		return nil
	})

	return templateErr
}