Browse Source

Add base template

main
Matt Baer 3 years ago
parent
commit
3218627068
3 changed files with 40 additions and 3 deletions
  1. +16
    -3
      templates.go
  2. +23
    -0
      templates/base.tmpl
  3. +1
    -0
      templates/profile.tmpl

+ 16
- 3
templates.go View File

@@ -4,23 +4,36 @@ import (
"html/template" "html/template"
"io" "io"
"log" "log"
"net/http"
) )


var profileTmpl *template.Template
var templates = map[string]*template.Template{}


const templatesDir = "templates/" const templatesDir = "templates/"


func init() { func init() {
profileTmpl = template.Must(template.New("profile").ParseFiles(templatesDir + "profile.tmpl"))
initTemplate("profile")
}

func initTemplate(name string) {
templates[name] = template.Must(template.New(name).ParseFiles(templatesDir+name+".tmpl", templatesDir+"base.tmpl"))
} }


// renderTemplate retrieves the given template and renders it to the given io.Writer. // renderTemplate retrieves the given template and renders it to the given io.Writer.
// If something goes wrong, the error is logged and returned. // If something goes wrong, the error is logged and returned.
func renderTemplate(w io.Writer, tmpl string, data interface{}) error { func renderTemplate(w io.Writer, tmpl string, data interface{}) error {
err := profileTmpl.ExecuteTemplate(w, tmpl, data)
err := templates[tmpl].ExecuteTemplate(w, tmpl, data)
if err != nil { if err != nil {
log.Printf("[ERROR] Error rendering %s: %s\n", tmpl, err) log.Printf("[ERROR] Error rendering %s: %s\n", tmpl, err)
} }


return err return err
} }

func (app *app) pageHandler(name string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
handleError(w, r, func() error {
return renderTemplate(w, name, nil)
}())
}
}

+ 23
- 0
templates/base.tmpl View File

@@ -0,0 +1,23 @@
{{define "pre-end-body"}}
<script type="text/javascript">
try { // Google Fonts
WebFontConfig = {
custom: { families: [ 'Open+Sans:400,700:latin' ], urls: [ '/css/fonts.css' ] }
};
(function() {
var wf = document.createElement('script');
wf.src = '/js/webfont.js';
wf.type = 'text/javascript';
wf.async = 'true';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(wf, s);
})();
} catch (e) { /* ¯\_(ツ)_/¯ */ }
</script>
{{end}}

{{define "footer"}}
<footer>
&copy; 2018 &ndash; 2021 <a href="https://abunchtell.com">A Bunch Tell [labs]</a>
</footer>
{{end}}

+ 1
- 0
templates/profile.tmpl View File

@@ -30,6 +30,7 @@
</div> </div>
</div> </div>
</div> </div>
{{template "pre-end-body" .}}
</body> </body>
</html> </html>
{{end}} {{end}}

Loading…
Cancel
Save