bou.ke/statictemplate@v0.0.0-20180821122055-510411a5e7dd/example/template/template.go (about)

     1  package template
     2  
     3  import (
     4  	pkg1 "bou.ke/statictemplate/example"
     5  	"bou.ke/statictemplate/funcs"
     6  	"io"
     7  )
     8  
     9  func Index(w io.Writer, dot []pkg1.Post) (err error) {
    10  	defer func() {
    11  		if recovered := recover(); recovered != nil {
    12  			var ok bool
    13  			if err, ok = recovered.(error); !ok {
    14  				panic(recovered)
    15  			}
    16  		}
    17  	}()
    18  	return fun0(w, dot)
    19  }
    20  
    21  // header.tmpl(string)
    22  func fun2(w io.Writer, dot string) error {
    23  	_, _ = io.WriteString(w, "<!doctype html>\n<html>\n  <head>\n    ")
    24  	if eval := dot; len(eval) != 0 {
    25  		_, _ = io.WriteString(w, "\n    <title>Bouke's Blog | ")
    26  		_, _ = io.WriteString(w, funcs.Rcdataescaper(dot))
    27  		_, _ = io.WriteString(w, "</title>\n    ")
    28  	} else {
    29  		_, _ = io.WriteString(w, "\n    <title>Bouke's Blog</title>\n    ")
    30  	}
    31  	_, _ = io.WriteString(w, "\n  </head>\n  <body>\n")
    32  	return nil
    33  }
    34  
    35  // post.tmpl(pkg1.Post)
    36  func fun3(w io.Writer, dot pkg1.Post) error {
    37  	_, _ = io.WriteString(w, "<article>\n  <h2>")
    38  	_, _ = io.WriteString(w, funcs.Htmlescaper(dot.Title))
    39  	_, _ = io.WriteString(w, "</h2>\n  <p>")
    40  	_, _ = io.WriteString(w, funcs.Htmlescaper(dot.Body))
    41  	_, _ = io.WriteString(w, "</h2>\n</article>\n")
    42  	return nil
    43  }
    44  
    45  // footer.tmpl(nil)
    46  func fun4(w io.Writer, dot interface{}) error {
    47  	_, _ = io.WriteString(w, "</body>\n</html>\n")
    48  	return nil
    49  }
    50  
    51  // index.tmpl([]pkg1.Post)
    52  func fun0(w io.Writer, dot []pkg1.Post) error {
    53  	if err := fun2(w, "Index"); err != nil {
    54  		return err
    55  	}
    56  	_, _ = io.WriteString(w, "\n\n<section>\n")
    57  	if eval := dot; len(eval) != 0 {
    58  		for _, _Varpost := range eval {
    59  			dot := _Varpost
    60  			_ = dot
    61  			_, _ = io.WriteString(w, "\n")
    62  			if err := fun3(w, _Varpost); err != nil {
    63  				return err
    64  			}
    65  			_, _ = io.WriteString(w, "\n")
    66  		}
    67  	}
    68  	_, _ = io.WriteString(w, "\n</section>\n\n")
    69  	if err := fun4(w, nil); err != nil {
    70  		return err
    71  	}
    72  	_, _ = io.WriteString(w, "\n")
    73  	return nil
    74  }